Reputation: 971
I have a .txt file which looks like the following:
156 2.87893e+06
157 968759
699 2.15891e+06
700 44927.2
1108 830338
1156 70513.5
1172 64263.2
The above file is an output file I am obtaining on running a c++ prog. I want this output file in pandas df.
I tried following code:
df = pd.read_csv("output.txt", index_col=0)
df
But here, columns from .txt file becomes one column in the df. I want the values in 2 columns separately, maybe with a column heading.
OR since its a text file, if they are not 2 different columns in .txt file, then each row has 2 values separated by a space. How can I get them in two different cols in pandas df ?
Also, I tried the following code:
df = pd.read_csv("output.txt")
df.iloc[:,(0)]
Now, here the very first row from the original text file doesn't appears at all, and again both the values appear in one column.
Upvotes: 1
Views: 5068
Reputation: 215047
The default delimiter for pandas.read_csv
is comma ,
, you need to explicitly specify the sep
parameter to be space in order to read in space delimited file:
df = pd.read_csv("output.txt", sep = " ", index_col=0, header=None)
Upvotes: 4