我有一个带有时间戳的文本文件,它看起来像这样:
00:25
hold it miles lunch and remember I'm
00:30
working late tonight again man you're a
00:34
total slave to that business of yours
00:36
nobody's a slave to their own dream
我正在尝试弄清楚如何将它导入 Pandas Dataframe,因此它看起来像这样:
[Time] [Text]
00:25 hold it miles lunch and remember I'm
00:30 working late tonight again man you're a
00:34 total slave to that business of yours
00:36 nobody's a slave to their own dream
我很尴尬地说我什至不知道从哪里开始......我知道并尝试过的所有方法都产生了这个:
row1 00:25
row2 hold it miles lunch and remember I'm
row3 00:30
row4 working late tonight again man you're a
row5 00:34
row6 total slave to that business of yours
row7 00:36
row8 nobody's a slave to their own dream
我找到了这个 question它看起来是同一个问题,但我不知道如何在创建数据框时应用它。
谢谢你帮助我!
最佳答案
下面是实现这个的方法:
# Import the sample data
data='''00:25
hold it miles lunch and remember I'm
00:30
working late tonight again man you're a
00:34
total slave to that business of yours
00:36
nobody's a slave to their own dream'''
# Create a list containing every line
data = data.split('\n')
# Parse the data, assigning every other row to a different column
col1 = [data[i] for i in range(0,len(data),2)]
col2 = [data[i] for i in range(1,len(data),2)]
# Create the data frame
df = pd.DataFrame({'Time': col1, 'Text': col2})
print(df)
Time Text
0 00:25 hold it miles lunch and remember I'm
1 00:30 working late tonight again man you're a
2 00:34 total slave to that business of yours
3 00:36 nobody's a slave to their own dream
关于Python Pandas 使用换行符拆分从文件创建数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55266444/