在PANDAS中对多个数据帧进行操作

时间:2019-06-03 12:12:57

标签: python pandas dataframe dictionary group-by

我有几个看起来像这样的表:

ID  YY  ZZ
 2  97  826
 2  78  489
 4  47  751
 4  110 322
 6  67  554
 6  88  714

代码:

raw = {'ID': [2, 2, 4, 4,  6, 6,],
   'YY': [97,78,47,110,67,88],
   'ZZ':[826,489,751,322,554,714]}
df = pd.DataFrame(raw)

对于每一个df,我必须执行许多操作。

  1. 首先,按ID分组
  2. 提取zz列的长度和zz列的平均值,
  3. 放入新df的结果

看起来像这样的新df

Cities  length  mean
Paris     0      0
Madrid    0      0
Berlin    0      0
Warsaw    0      0
London    0      0

代码:

raw2 = {'Cities': ['Paris', 'Madrid', 'Berlin',  'Warsaw', 'London'],
   'length': 0,
   'mean': 0}
df2 = pd.DataFrame(raw2)

我提取了各个组的平均值和大小

df_grouped = df.groupby('ID').ZZ.size()
df_grouped2 = df.groupby('ID').ZZ.mean()

尝试将结果转移到新表时会发生问题,因为它不包含所有城市,并且必须根据适当​​的键来匹配结果。

我尝试使用字典:

   dic_cities = {"Paris":df_grouped.loc[2],
             "Madrid":df_grouped.loc[4],
             "Warsaw":df_grouped.loc[6],
             "Berlin":df_grouped.loc[8],
             "London":df_grouped.loc[10]}

很遗憾,我收到KeyError:8

我有19个df,必须从中提取这些数据,并且最终表必须如下所示:

    Cities  length  mean
    Paris   2       657.5
    Madrid  2       536.5
    Berlin  0       0.0
    Warsaw  2       634.0
    London  0       0.0

有人知道如何使用groupby和字典来处理它,还是知道一种更好的方法?

2 个答案:

答案 0 :(得分:1)

看到这个:

import pandas as pd

# setup raw data
raw = {'ID': [2, 2, 4, 4,  6, 6,], 'YY': [97,78,47,110,67,88], 'ZZ':[826,489,751,322,554,714]}
df = pd.DataFrame(raw)

# get mean values
mean_values = df.groupby('ID').mean()
# drop column
mean_values = mean_values.drop(['YY'], axis=1)
# get occurrence number
occurrence = df.groupby('ID').size()
# save data
result = pd.concat([occurrence, mean_values], axis=1, sort=False)
# rename columns
result.rename(columns={0:'length', 'ZZ':'mean'}, inplace=True)

# city data
raw2 = 'Cities': ['Paris', 'Madrid', 'Berlin',  'Warsaw', 'London'], 'length': 0, 'mean': 0}
df2 = pd.DataFrame(raw2)

# rename indexes
df2 = df2.rename(index={0: 2, 1:4, 2:8, 3:6, 4:10}

# merge data
df2['length'] = result['length']
df2['mean'] = result['mean']

出局:

    Cities  length   mean
2    Paris     2.0  657.5
4   Madrid     2.0  536.5
8   Berlin     NaN    NaN
6   Warsaw     2.0  634.0
10  London     NaN    NaN

答案 1 :(得分:1)

首先,您应该在df2上索引'Cities'

raw2 = {'Cities': ['Paris', 'Madrid', 'Berlin',  'Warsaw', 'London'],
   'length': 0,
   'mean': 0}
df2 = pd.DataFrame(raw2).set_index('Cities')

然后您应该反向词典:

dic_cities = {2: "Paris",
             4: "Madrid",
             6: "Warsaw",
             8: "Berlin",
             10: "London"}

完成此操作后,处理就像groupby一样简单:

for i, sub in df.groupby('ID'):
    df2.loc[dic_cities[i]] = sub.ZZ.agg([len, np.mean]).tolist()

哪个给df2

        length   mean
Cities               
Paris      2.0  657.5
Madrid     2.0  536.5
Berlin     0.0    0.0
Warsaw     2.0  634.0
London     0.0    0.0