imtiaz ul Hassan
imtiaz ul Hassan

Reputation: 388

How to parse Pandas columns from a text file?

I have a text file and I want to separate each column. But the whole text is treated as one column by pandas:

   6/1/2018 12:01:11.490 AM HEP.U02.OIL.GOV.P2_RUN  <Unit #2>No.2 oil pump 
   runing   On      
   6/1/2018 12:01:19.383 AM HEP.COM.WAT.DRN.P1_CRTL Control No.1 drainage 
   pump On      
   6/1/2018 12:01:19.384 AM HEP.COM.WAT.DRN.P1_RUN  No.1 drainage pump run   
   On       
   6/1/2018 12:01:24.250 AM HEP.U02.OIL.GOV.LV_TL   <Unit #2>Oil level of 
   sump too low Off     
   6/1/2018 12:01:24.859 AM HEP.U02.OIL.GOV.P2_RUN  <Unit #2>No.2 oil pump 
   runing   Off     
   6/1/2018 12:02:07.564 AM HEP.U02.OIL.GOV.LV_TL   <Unit #2>Oil level of 
   sump too low On

This how my data looks:

import re
import pandas as pd
p=[] 
df=pd.read_csv('./mine.txt',encoding='ISO-8859-1', sep='delimeter', 
engine='python' ,nrows=100)
print(df.shape)
df=df.columns.str.split('\t')
print(list(df))
print(df.shape)

df.shape() returns (100,1) before and (1,) after str.split()

How can I separate these columns ?

Upvotes: 0

Views: 407

Answers (1)

Michal Mikul&#225;ši
Michal Mikul&#225;ši

Reputation: 26

You are separating your data on tabs, not spaces. If your file would be .tsv(tab separated values) it would make sense. You should separate it with "\t", but if not, you should use "\s" to separate on spaces, not tabs. And thats why you only get (1,) after str.split("\t").

Upvotes: 1

Related Questions