总结大量数据帧

时间:2017-04-10 18:50:20

标签: python pandas dataframe

我有大量具有完全相同的键和列名称的pandas数据帧。他们的数据如下:

z1.ix[0]
val1        [1, 5, 3, 4]
val2        47
Name: 2017-01-01 01:00:00, dtype: object

z2.ix[0]
val1        [11, 5, 53, 5]
val2        4
Name: 2017-01-01 01:00:00, dtype: object

z3.ix[0]
val1        [1, 25, 3, 4]
val2        7
Name: 2017-01-01 01:00:00, dtype: object

我尝试了以下内容:

summedDf = z1 + z2 + z3

给出以下内容:

summedDf.ix[0]
val1        [1, 5, 3, 4, 11, 5, 53, 5, 1, 25, 3, 4]
val2        58
Name: 2017-01-01 01:00:00, dtype: object

但我希望能够实现以下目标:

summedDf.ix[0]
val1        [13, 35, 59, 13]
val2        58
Name: 2017-01-01 01:00:00, dtype: object

另外,如何将上述添加扩展到大约500个数据帧?

编辑: val1val2是不同的列名。 val1存储每个索引的列表和val2存储值。

1 个答案:

答案 0 :(得分:0)

可能不是最有效的,但会让你开始:

import pandas as pd
import numpy as np


# gen test data
df1 = pd.DataFrame({'val1':[[1,2,3],[4,5,6]], 'val2': [1,2]})
df1

给出,

    val1       val2
0   [1, 2, 3]   1
1   [4, 5, 6]   2

另一个数据框:

def check(x):
    if isinstance(x, list):
        output = [i * 2 for i in x]
    else:
        output = x*2
    return output

df2 = df1.applymap(lambda x: check(x))
df2

给出,

    val1       val2
0   [2, 4, 6]   2
1   [8, 10, 12] 4

添加数据框:

def add_cols(df1, df2, col):
    if isinstance(df1[col][0], list):
        df1[col] = df1[col].apply(lambda x: np.array(x))
        df2[col] = df2[col].apply(lambda x: np.array(x))
    return df1[col].add(df2[col])


def add_dfs(df1, df2):
    for c in df1.columns:
        df1.loc[:,c] = add_cols(df1, df2, c)
    return df1


# you can use a generator to read dataframes on the fly
# instead of loading all into a list
dfs = [df1, df2]


for e, df in enumerate(dfs):
    if e == 0:
        df_sum = df.copy()
    else:
        df_sum = add_dfs(df1, df2)

给出所需的输出:

    val1          val2
0   [5, 10, 15]     5
1   [20, 25, 30]    10