简体   繁体   中英

Read 4 lines of data into one row of pandas data frame

I have txt file with such values:

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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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