简体   繁体   中英

Reading one 'cell' of a fixed width table (.txt file) that is split over two lines in python/pandas

How do I read one "cell" of a fixed width column that is split over two lines? The data input is a fixed width table, like so;

ID   Description                 QTY
1    Description split over      1
     two lines
2    Description on one line     2

I'd like to have the data frame format the data as per below;

ID   Description                           QTY
1    Description split over two lines      1       
2    Description on one line               2

My current code is;

import pandas as pd

df = pd.read_fwf('test.txt', names = ['ID', 'Description', 'QTY'])
df

But this gives me;

ID   Description                 QTY
1    Description split over      1
NaN  two lines                   NaN 
2    Description on one line     2

Any ideas?

#Conditionally concatenate description from next row to current row if the ID of next row is NAN>
df['Description'] = df.apply(lambda x: x.Description if x.name==(len(df)-1) else x.Description + ' ' + df.iloc[x.name+1]['Description'] if np.isnan(df.iloc[x.name+1]['ID']) else x.Description, axis=1)

#Drop rows with NA.
df = df.dropna()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM