阅读一';单元格';一个固定宽度的表格(.txt文件),在python/pandas中分为两行

阅读一';单元格';一个固定宽度的表格(.txt文件),在python/pandas中分为两行,python,pandas,Python,Pandas,如何读取拆分为两行的固定宽度列中的一个“单元格”?数据输入是一个固定宽度的表格,如下所示 ID Description QTY 1 Description split over 1 two lines 2 Description on one line 2 我想有数据帧格式的数据如下 ID Description QTY 1 Description spli

如何读取拆分为两行的固定宽度列中的一个“单元格”?数据输入是一个固定宽度的表格,如下所示

ID   Description                 QTY
1    Description split over      1
     two lines
2    Description on one line     2
我想有数据帧格式的数据如下

ID   Description                           QTY
1    Description split over two lines      1       
2    Description on one line               2
我现在的代码是

import pandas as pd

df = pd.read_fwf('test.txt', names = ['ID', 'Description', 'QTY'])
df
但这给了我

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

有什么想法吗?

Related:,不确定这在Pandas中是否可行,可能只需要一些基于正则表达式的脚本就可以将文件转换为更正常的文件,这样就不会有任何东西像这样溢出到行中。Related:,不确定这在Pandas中是否可行,它可能只需要一些基于正则表达式的脚本就可以将您的文件转换为更普通的文件,这样就不会有任何东西像那样溢出到行中。太好了!非常感谢:)太好了!非常感谢:)
#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()