大熊猫替换列某些条件值

时间:2018-10-17 02:47:14

标签: pandas dataframe replace

DataFrame“数据”

temp = data[(data['block_name']=='坪山') & (data['community_id']!=186)][['community_id','block_name','dstrct_name','month_name','avg_price']]\
.loc[data['month_name'].isin(data[(data['community_id']==186) & (data['avg_price'].isnull())]['month_name'])]\
.groupby(['month_name']).agg(np.mean).reset_index()
temp

输出:

month_name  avg_price
0   2017-06 24796.0
1   2017-07 25169.5
2   2017-08 24253.0
3   2017-09 25838.0
4   2017-10 26275.0
5   2017-11 26592.0
6   2017-12 26783.0

现在我要使用上面的结果来填充下一个:

data.loc[(data['community_id']==186) & (data['avg_price'].isnull()),['month_name','avg_price']]

其输出:

month_name  avg_price
70  2017-06 NaN
71  2017-07 NaN
72  2017-08 NaN
73  2017-09 NaN
74  2017-10 NaN
75  2017-11 NaN
76  2017-12 NaN

我尝试这样做:

data.loc[(data['community_id']==186) & (data['avg_price'].isnull()),'avg_price'] = data[(data['block_name']=='坪山') & (data['community_id']!=186)][['community_id','block_name','dstrct_name','month_name','avg_price']]\
.loc[dd['month_name'].isin(data[(data['community_id']==186) & (data['avg_price'].isnull())]['month_name'])]\
.groupby(['month_name']).agg(np.mean).reset_index()['avg_price']

然后我跑步

data.loc[(data['community_id']==186) & (data['avg_price'].isnull()),['month_name','avg_price']]

再次发现它不起作用

month_name  avg_price
70  2017-06 NaN
71  2017-07 NaN
72  2017-08 NaN
73  2017-09 NaN
74  2017-10 NaN
75  2017-11 NaN
76  2017-12 NaN

我只想更改“数据”。

0 个答案:

没有答案