Python Pandas 数据框 fillna() 只有一些列就位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38134012/
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
Pandas dataframe fillna() only some columns in place
提问by Sait
I am trying to fill none values in a Pandas dataframe with 0's for only some subset of columns.
我试图在 Pandas 数据框中只为某些列子集填充 0 值。
When I do:
当我做:
import pandas as pd
df = pd.DataFrame(data={'a':[1,2,3,None],'b':[4,5,None,6],'c':[None,None,7,8]})
print df
df.fillna(value=0, inplace=True)
print df
The output:
输出:
a b c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 NaN 7.0
3 NaN 6.0 8.0
a b c
0 1.0 4.0 0.0
1 2.0 5.0 0.0
2 3.0 0.0 7.0
3 0.0 6.0 8.0
It replaces every Nonewith 0's. What I want to do is, only replace Nones in columns aand b, but not c.
它None用0's替换 each 。我想要做的是,只替换None列中的 sa和b,而不是c.
What is the best way of doing this?
这样做的最佳方法是什么?
回答by root
You can select your desired columns and do it by assignment:
您可以选择所需的列并通过分配来完成:
df[['a', 'b']] = df[['a','b']].fillna(value=0)
The resulting output is as expected:
结果输出如预期:
a b c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 0.0 7.0
3 0.0 6.0 8.0
回答by YOBEN_S
You can using dict, fillnawith different value for different column
您可以使用dict,fillna不同列的不同值
df.fillna({'a':0,'b':0})
Out[829]:
a b c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 0.0 7.0
3 0.0 6.0 8.0
After assign it back
分配回来后
df=df.fillna({'a':0,'b':0})
df
Out[831]:
a b c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 0.0 7.0
3 0.0 6.0 8.0
回答by Leesa H.
You can avoid making a copy of the object using Wen's solution and inplace=True:
您可以避免使用 Wen 的解决方案和 inplace=True 制作对象的副本:
df.fillna({'a':0, 'b':0}, inplace=True)
print(df)
Which yields:
其中产生:
a b c
0 1.0 4.0 NaN
1 2.0 5.0 NaN
2 3.0 0.0 7.0
3 0.0 6.0 8.0
回答by Josephine M. Ho
Here's how you can do it all in one line:
以下是如何在一行中完成所有操作:
df[['a', 'b']].fillna(value=0, inplace=True)
Breakdown: df[['a', 'b']]selects the columns you want to fill NaN values for, value=0tells it to fill NaNs with zero, and inplace=Truewill make the changes permanent, without having to make a copy of the object.
细分:df[['a', 'b']]选择要为其填充 NaN 值的列,value=0告诉它用零填充 NaN,inplace=True并使更改永久化,而无需复制对象。
回答by Jonathan
using the top answer produces a warning about making changes to a copy of a df slice. Assuming that you have other columns, a better way to do this is to pass a dictionary: df.fillna({'A': 'NA', 'B': 'NA'}, inplace=True)
使用最佳答案会产生关于更改 df 切片副本的警告。假设您有其他列,更好的方法是传递字典:df.fillna({'A': 'NA', 'B': 'NA'}, inplace=True)
回答by U10-Forward
Or something like:
或类似的东西:
df.loc[df['a'].isnull(),'a']=0
df.loc[df['b'].isnull(),'b']=0
and if there is more:
如果还有更多:
for i in your_list:
df.loc[df[i].isnull(),i]=0
回答by Sarath Baby
Sometimes this syntax wont work:
有时这种语法不起作用:
df[['col1','col2']] = df[['col1','col2']].fillna()
Use the following instead:
请改用以下内容:
df['col1','col2']

