converting dataframe to int numpy array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: converting dataframe to int numpy array (/thread-25582.html) |
converting dataframe to int numpy array - glennford49 - Apr-04-2020 i have a text file with 3 lines : 1,2,3 1,2,4 1,2,5 import pandas as pd import numpy as np df= pd.read_fwf("sample.txt",header =None) df= df.to_numpy() print("df:",df)running my code gives me a result of df:[['1,2,3'] ['1,2,4'] ['1,2,5']] how to convert it to integers? i want the output to be [[1,2,3] [1,2,4] [1,2,5]] RE: converting dataframe to int numpy array - snippsat - Apr-04-2020 Use reaf_csv() . >>> import pandas as pd >>> import numpy as np >>> df = pd.read_csv("sample.txt", header=None, sep=',') >>> df = df.to_numpy() >>> df array([[1, 2, 3], [1, 2, 4], [1, 2, 5]], dtype=int64) >>> df[0] array([1, 2, 3], dtype=int64) >>> df[0][2] 3 >>> type(df[0][2]) <class 'numpy.int64'> |