2

我有两个数据框。除了一列之外,它们是相同的。我想根据第一个数据帧的平均值更改第二个数据帧的列。对于后者,我必须使用 groupby,但是我不知道如何获得反向。下面是一个最小示例,在此特定示例中,df_two 最终应该与 df_one 相同。我的问题是如何从 tmp 到 df2_new - 请参阅下面的代码。

import pandas as pd


def foo(df1, df2):
    # Group by A
    groupsA_one = dict(list(df1.groupby('A', as_index=False)))
    groupsA_two = dict(list(df2.groupby('A', as_index=False)))

    for key_A in groupsA_one:
        # Group by B
        groupsB_one = dict(list(groupsA_one[key_A].groupby('B', as_index=False)))
        groupsB_two = dict(list(groupsA_two[key_A].groupby('B', as_index=False)))

        for key_B in groupsB_one:
            # Group by C
            tmp = groupsB_two[key_B].groupby('C', as_index=False)['D'].mean()   # Returns DataFrame with NaN
            tmp['D'] = groupsB_one[key_B].groupby('C', as_index=False)['D'].mean()['D']
            print tmp

    df2_new = []        # ???
    return df2_new

if __name__ == '__main__':
    A1 = {'A': [1, 1, 1, 1, 2, 2, 2, 2], 'B': [1, 1, 2, 2, 1, 1, 2, 2],
          'C': [1, 2, 1, 2, 1, 2, 1, 2], 'D': [5, 5, 5, 5, 5, 5, 5, 5]}
    A2 = {'A': [1, 1, 1, 1, 2, 2, 2, 2], 'B': [1, 1, 2, 2, 1, 1, 2, 2],
          'C': [1, 2, 1, 2, 1, 2, 1, 2], 'D': [0, 0, 0, 0, 0, 0, 0, 0]}
    df_one = pd.DataFrame(A1)
    df_two = pd.DataFrame(A2)
    foo(df_one, df_two)
4

3 回答 3

4

我认为在某些情况下这可能更简单:

groupby = dfm.groupby('variable') for ix, row in reversed(tuple(groupby)): ...

于 2017-12-16T21:18:36.420 回答
0

这是我想要的解决方案。请,如果您找到更优雅的解决方案,我很乐意将其设置为正确答案。

它是:

import pandas as pd
import numpy as np


def foo(df):
    # Group by A
    groups_a_one = dict(list(df.groupby('A', as_index=False)))

    for key_a in groups_a_one:
        # Group by B
        groups_b_one = dict(list(groups_a_one[key_a].groupby('B', as_index=False)))

        for key_b in groups_b_one:
            # Group by C
            tmp = groups_b_one[key_b].groupby('C', as_index=False).transform(lambda x: x.fillna(x.mean()))
            df.ix[tmp.index, 'D'] = tmp['D']# assign mean values to correct lines in df

    return df

if __name__ == '__main__':
    A1 = {'A': [1, 1, 1, 1, 2, 2, 2, 2], 'B': [1, 1, 2, 2, 1, 1, 2, 2],
          'C': [1, 2, 1, 2, 1, 2, 1, 2], 'D': [5, 5, 5, 5, 5, 5, 5, 5]}
    A2 = {'A': [1, 1, 1, 1, 2, 2, 2, 2], 'B': [1, 1, 2, 2, 1, 1, 2, 2],
          'C': [1, 2, 1, 2, 1, 2, 1, 2], 'D': [np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN]}
    df_one = pd.DataFrame(A1)
    df_two = pd.DataFrame(A2)
    df = pd.concat([df_one, df_two], axis=0, ignore_index=True)# To get only one DataFrame

    # run the transform
    foo(df)

这是初始状态和最终状态:

# Initial
    A  B  C  D
0   1  1  1  5
1   1  1  2  5
2   1  2  1  5
3   1  2  2  5
4   2  1  1  5
5   2  1  2  5
6   2  2  1  5
7   2  2  2  5
8   1  1  1  NaN
9   1  1  2  NaN
10  1  2  1  NaN
11  1  2  2  NaN
12  2  1  1  NaN
13  2  1  2  NaN
14  2  2  1  NaN
15  2  2  2  NaN

# Final
    A  B  C  D
0   1  1  1  5
1   1  1  2  5
2   1  2  1  5
3   1  2  2  5
4   2  1  1  5
5   2  1  2  5
6   2  2  1  5
7   2  2  2  5
8   1  1  1  5
9   1  1  2  5
10  1  2  1  5
11  1  2  2  5
12  2  1  1  5
13  2  1  2  5
14  2  2  1  5
15  2  2  2  5
于 2014-07-18T10:17:47.810 回答
0
#Do a group by on df_one on A, B, and C and find the mean
    df_group = df_one.groupby(['A','B','C']).mean()

#Change the index
    df_two.index = [df_two['A'],df_two['B'],df_two['C']]

#Transfer the value of mean from D to 
    df_two['D'] = df_group['D']
于 2017-12-17T06:02:23.497 回答