简体   繁体   English

创建 pandas dataframe from.txt 文件,每行数据

[英]Create pandas dataframe from .txt file with data per row

I have a.txt file with data ordered as below:我有一个 .txt 文件,其中的数据排序如下:

R11
R12
R13
R14
R15
R16
R17
R18
R19
R20

I need to iterate over rows in the textfile to fill the columns per row.我需要遍历文本文件中的行以填充每行的列。 In other words, the data need to be transformed to a pandas DataFrame looking like this:换句话说,需要将数据转换为 pandas DataFrame,如下所示:

| Column1 | Column2 | Column3 | Column4 | Column5 |
|---------|---------|---------|---------|---------|
| R11     | R12     | R13     | R14     | R15     |
| R16     | R17     | R18     | R19     | R20     |

My code starts with the following.我的代码从以下开始。 After running, I now have a list of all rows called data , but how to get a pandas DataFrame as above as output?运行后,我现在有一个名为data的所有行的列表,但是如何获得 pandas DataFrame 和上面的 output 一样?

with open('data.txt','r') as file:
    data = file.read().split('\n')

You can read text file using pd.read_csv and name column name您可以使用pd.read_csv和名称column名称读取文本文件

pd.DataFrame(pd.read_csv('data.txt', names=[0]).values.reshape(-1,5), columns = ['Column1','Column2', 'Column3', 'Column4', 'Column4'])

在此处输入图像描述

You are almost there!你快到了! After reading the data in as a list you can split the list in chunk of 5s and then pass it in the pd.DataFrame()将数据作为列表读取后,您可以将列表拆分为 5 秒,然后将其传递给pd.DataFrame()

with open('data.txt','r') as file:
    data = file.read().split('\n')

# split the list in chunks of 5s
chunks = [data[x:x+5] for x in range(0, len(data), 5)]

# pass the chunks in pd.DataFrame and specify the columns names of the OP:
pd.DataFrame(chunks, columns=["Column1", "Column2", "Column3", "Column4", "Column5"])

PS I assumed there was a typo in the last column as it appears to be named again Column4, but here I named it Column5. PS 我以为最后一列有错字,因为它似乎又被命名为 Column4,但在这里我将其命名为 Column5。 You can always name it Column4 if this is what you needed.如果这是您需要的,您始终可以将其命名为 Column4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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