[英]Read 4 lines of data into one row of pandas data frame
I have txt file with such values: 我有这样的值的txt文件:
108,612,620,900
168,960,680,1248
312,264,768,564
516,1332,888,1596
I need to read all of this into a single row of data frame. 我需要将所有这些读入一行数据帧。
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 108 612 620 900 168 960 680 1248 312 264 768 564 516 1332 888 1596
I have many such files and so I'll keep appending rows to this data frame. 我有很多这样的文件,所以我会继续向这个数据框追加行。
I believe we need some kind of regex but I'm not able to figure it out. 我相信我们需要某种正则表达式,但我无法弄明白。 For now this is what I have :
现在这就是我所拥有的:
df = pd.read_csv(f,sep=",| ", header = None)
But this takes ,
and 但这需要
,
并且 (space) as separators where as I want it to take newline as a separator.
(空格)作为分隔符,我希望它将换行作为分隔符。
First, read the data: 首先,阅读数据:
df = pd.read_csv('test/t.txt', header=None)
It gives you a DataFrame shaped like the CSV. 它为您提供了一个像CSV一样的DataFrame。 Then concatenate:
然后连接:
s = pd.concat((df.loc[i] for i in df.index), ignore_index=True)
It gives you a Series: 它给你一个系列:
0 108
1 612
2 620
3 900
4 168
5 960
6 680
7 1248
8 312
9 264
10 768
11 564
12 516
13 1332
14 888
15 1596
dtype: int64
Finally, if you really want a horizontal DataFrame: 最后,如果你真的想要一个水平DataFrame:
pd.DataFrame([s])
Gives you: 给你:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 108 612 620 900 168 960 680 1248 312 264 768 564 516 1332 888 1596
Since you've mentioned in a comment that you have many such files, you should simply store all the Series in a list, and construct a DataFrame with all of them at once when you're finished loading them all. 由于您在评论中提到您有许多此类文件,因此您只需将所有系列存储在一个列表中,并在完成全部加载后立即构建一个包含所有这些文件的DataFrame。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.