Reputation:
i have text file which contains following information
# Column1 Column2
10 12
15 7
20 9
30 11
40 20
60 25
i have tried to read this file like this
import numpy as np
import pandas as pd
file =pd.read_table("results.txt",sep="\t")
print(file.head())
result is this :
# Column1 Column2
10 12
15 7
20 9
30 11
40 20
of course i can use skiprows to skip first rows like this
import numpy as np
import pandas as pd
file =pd.read_table("results.txt",sep="\t",skiprows=1,header=None)
print(file.head())
result is :
0 1
0 10 12
1 15 7
2 20 9
3 30 11
4 40 20
but i want to keep original columns, how to do it? How to separate columns well?
Upvotes: 0
Views: 92
Reputation: 1731
This should do it.
Read in your data using read_csv
use the Sep=" "
so as to use spaces.
Input is messy so a little cleanup. Drop the unwanted column1 which will be full of NAs. Then rename the column holding the data you want to call column1.
import pandas as pd
data = pd.read_csv(r"results.txt", sep=" ")
data.drop('Column1', inplace=True, axis=1)
data.rename(columns={'#': 'Column1'}, inplace=True)
print(data)
output
Column1 Column2
0 10 12
1 15 7
2 20 9
3 30 11
4 40 20
5 60 25
Upvotes: 1