pandas 用零 python 熊猫填充 nan
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52835971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill nan with zero python pandas
提问by NilZ
this is my code:
这是我的代码:
for col in df:
if col.startswith('event'):
df[col].fillna(0, inplace=True)
df[col] = df[col].map(lambda x: re.sub("\D","",str(x)))
I have 0 to 10 event column "event_0, event_1,..." When I fill nan with this code it fills all nan cells under all event columns to 0 but it does not change event_0 which is the first column of that selection and it is also filled by nan.
我有 0 到 10 个事件列“event_0, event_1,...”也被nan填了。
I made these columns from 'events' column with following code:
我使用以下代码从“事件”列制作了这些列:
event_seperator = lambda x: pd.Series([i for i in
str(x).strip().split('\n')]).add_prefix('event_')
df_events = df['events'].apply(event_seperator)
df = pd.concat([df.drop(columns=['events']), df_events], axis=1)
Please tell me what is wrong? you can see dataframe before changing in the picture.
请告诉我有什么问题?您可以在更改图片之前看到数据框。
回答by jpp
I don't know why that happened since I made all those columns the same.
我不知道为什么会发生这种情况,因为我使所有这些列都相同。
Your data suggests this is precisely what has notbeen done.
您的数据表明,这正是一直没有做过。
You have a few options depending on what you are trying to achieve.
根据您要实现的目标,您有几种选择。
1. Convert all non-numeric values to 0
1. 将所有非数字值转换为 0
Use pd.to_numeric
with errors='coerce'
:
使用pd.to_numeric
有errors='coerce'
:
df[col] = pd.to_numeric(df[col], errors='coerce').fillna(0)
2. Replace either string ('nan') or null (NaN) values with 0
2. 将字符串 ('nan') 或空 (NaN) 值替换为 0
Use pd.Series.replace
followed by the previous method:
使用pd.Series.replace
后跟上一个方法:
df[col] = df[col].replace('nan', np.nan).fillna(0)