![](/img/trans.png)
[英]Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe
[英]Python Pandas: Fill dataframe with the same category name (in one column) and NaN (in another)
我有一个与此类似的数据框(但更大):
year city_code total_tax
id_inf
9 2002 NaN NaN
9 2003 a 417.0
9 2004 a 950.0
9 2005 NaN NaN
9 2006 NaN NaN
54 2002 b 801.0
54 2003 NaN NaN
54 2004 b 218.0
54 2005 b 886.0
54 2006 b 855.0
我需要使用相同类别的“ id_inf”填充“ city_code”,并在“ total_tax”列中将NaN替换为零。
第二个任务很容易: df_balanced['total_tax'] = df_balanced['total_tax'].fillna(0)
但是对于第一个任务,建议我使用类似的东西:'df_balanced ['city_code'] = df_balanced.groupby(level = 0)['city_code']。transform(max)'。 但是,当我使用此解决方案时,出现了以下错误: TypeError: '>=' not supported between instances of 'float' and 'str'
我需要的输出是这样的:
year city_code total_tax
id_inf
9 2002 a 0.0
9 2003 a 417.0
9 2004 a 950.0
9 2005 a 0.0
9 2006 a 0.0
54 2002 b 801.0
54 2003 b 0.0
54 2004 b 218.0
54 2005 b 886.0
54 2006 b 855.0
first
使用
df['city_code']=df.groupby('id_inf').city_code.transform('first')
#df.groupby('id_inf').city_code.transform('first')
Out[278]:
id_inf
9 a
9 a
9 a
9 a
9 a
54 b
54 b
54 b
54 b
54 b
Name: city_code, dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.