3

全部,

我有四列的数据框('key1'、'key2'、'data1'、'data2')。我在data1中插入了一些nan。现在我想用我做之后每个组中出现次数最多的值填充 nan groupby(['key1', 'key2'])

dt =  pd.DataFrame ({'key1': np.random.choice(['a', 'b'], size=100),
                 'key2': np.random.choice(['c', 'd'], size=100),
                  'data1': np.random.randint(5, size=100),
                  'data2': np.random.randn(100)},
                columns = ['key1', 'key2','data1', 'data2'])
#insert nan 
dt['data1'].ix[[2,6,10]]= None
# group by key1 and key2
group =dt.groupby(['key1', 'key2'])['data1']

group.value_counts(dropna=False)
key1  key2  data1
a     c     1.0       8
            4.0       6
            0.0       4
            2.0       2
            3.0       1
      d     0.0       7
            1.0       6
            4.0       6
            2.0       5
            NaN       3
            3.0       1
b     c     0.0       7
            2.0       7
            1.0       3
            3.0       2
            4.0       2
      d     2.0      11
            1.0      10
            0.0       3
            3.0       3
            4.0       3

对于这个例子,我要做的是用 0.0 填充 data1 列中的 nan(组内最常见的值(key1=a,key2=d)。

非常感谢您的帮助!

4

1 回答 1

3

利用.transform(lambda y: y.fillna(y.value_counts().idxmax()))

key1  key2  data1
a     c     1.0       6
            3.0       5
            0.0       4
            2.0       3
            4.0       3
            NaN       1
      d     1.0      11
            3.0       9
            0.0       5
            2.0       5
            4.0       5
b     c     4.0       7
            0.0       4
            3.0       4
            2.0       3
            NaN       2
            1.0       1
      d     4.0       6
            1.0       5
            2.0       5
            3.0       4
            0.0       2
Name: data1, dtype: int64

申请后.transform(lambda y: y.fillna(y.value_counts().idxmax()))

dt['nan_filled'] = dt.groupby(['key1', 'key2'])['data1'].transform(lambda y: y.fillna(y.value_counts().idxmax()))
group = dt.groupby(['key1', 'key2'])['nan_filled']
group.value_counts(dropna=False)


key1  key2  nan_filled
a     c     1.0            7
            3.0            5
            0.0            4
            2.0            3
            4.0            3
      d     1.0           11
            3.0            9
            0.0            5
            2.0            5
            4.0            5
b     c     4.0            9
            0.0            4
            3.0            4
            2.0            3
            1.0            1
      d     4.0            6
            1.0            5
            2.0            5
            3.0            4
            0.0            2
Name: nan_filled, dtype: int64
于 2016-10-07T15:24:17.307 回答