且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在循环中附加多个 pandas 数据帧?

更新时间:2023-11-18 21:38:46

我建议首先将所有数据帧与 pd.concat 连接在一起,然后再做一个最终的 pivot代码>操作.

filenames = sorted(glob.glob('*.csv'))new = [pd.read_csv(f, parse_dates=['time_stamp']) for f in filenames]df = pd.concat(new) # 省略轴参数,因为它默认为 0df = df.pivot(index='time_stamp', columns='pod')

请注意,当加载数据帧时,我强制read_csv解析time_stamp,因此不再需要在加载后解析.>

MCVE

dfpod 时间戳值0 97 2016-02-22 3.0480001 97 2016-02-29 23.6220012 97 2016-03-07 13.9700013 97 2016-03-14 6.6040004 97 2016-03-21 NaNdf.pivot(index='time_stamp', columns='pod')价值吊舱 97时间戳2016-02-22 3.0480002016-02-29 23.6220012016-03-07 13.9700012016-03-14 6.6040002016-03-21 NaN

I've been banging my head on this python problem for a while and am stuck. I am for-looping through several csv files and want one data frame that appends the csv files in a way that one column from each csv file is a column name and sets a common index of a date_time.

There are 11 csv files that look like this data frame except for different value and pod number, but the time_stamp is the same for all the csvs.

data

    pod time_stamp  value
0   97  2016-02-22  3.048000
1   97  2016-02-29  23.622001
2   97  2016-03-07  13.970001
3   97  2016-03-14  6.604000
4   97  2016-03-21  NaN

And this is the for-loop that I have so far:

import glob
import pandas as pd

filenames = sorted(glob.glob('*.csv'))

new = []

for f in filenames:
    data = pd.read_csv(f)

    time_stamp = [pd.to_datetime(d) for d in time_stamp]

    new.append(data)

my_df = pd.DataFrame(new, columns=['pod','time_stamp','value'])

What I want is a data frame that looks like this where each column is the result of value from each of the csv files.

time_stamp  97        98       99 ...
2016-02-22  3.04800   4.20002  3.5500
2016-02-29. 23.62201  24.7392  21.1110
2016-03-07 13.97001   11.0284  12.0000

But right now the output of my_df is very wrong and looks like this. Any ideas of where I went wrong?

    0
0   pod time_stamp value 0 22 2016-...
1   pod time_stamp value 0 72 2016-...
2   pod time_stamp value 0 79 2016-0...
3   pod time_stamp value 0 86 2016-...
4   pod time_stamp value 0 87 2016-...
5   pod time_stamp value 0 88 2016-...
6   pod time_stamp value 0 90 2016-0...
7   pod time_stamp value 0 93 2016-0...
8   pod time_stamp value 0 95 2016-...

I'd recommend first concatenating all your dataframes together with pd.concat, and then doing one final pivot operation.

filenames = sorted(glob.glob('*.csv'))

new = [pd.read_csv(f, parse_dates=['time_stamp']) for f in filenames]
df = pd.concat(new) # omit axis argument since it is 0 by default

df = df.pivot(index='time_stamp', columns='pod')

Note that I'm forcing read_csv to parse time_stamp when loading the dataframe, so parsing after loading is no longer required.


MCVE

df

   pod  time_stamp      value
0   97  2016-02-22   3.048000
1   97  2016-02-29  23.622001
2   97  2016-03-07  13.970001
3   97  2016-03-14   6.604000
4   97  2016-03-21        NaN

df.pivot(index='time_stamp', columns='pod')

                value
pod                97
time_stamp           
2016-02-22   3.048000
2016-02-29  23.622001
2016-03-07  13.970001
2016-03-14   6.604000
2016-03-21        NaN