Python Pandas 使用换行符拆分从文件创建数据框?

标签 python pandas dataframe

我有一个带有时间戳的文本文件,它看起来像这样:

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/

相关文章:

python - WinError 2:系统找不到在Python中用FluidSynth指定的文件吗?

python - 从 Python 中的切片对象中检索切片的长度

python - 按排序顺序检索 sqlalchemy 关系对象

python - pandas for loop,适用于小数据帧卡在大

python - 如何使用没有孔的 TimeSeries 为 "holes"索引 DataFrame

python - 如何将字符串作为新列添加到 Pandas Dataframe?

python - 替换 Python DataFrame 列中的字符

python - 将整数转换为负数

将 NA 值替换为组中的数值

r - 检查 data.frame 列中的任何值是否为空