简体   繁体   English

熊猫如何在列上填充文本?

[英]Pandas how to fillna in place on a column?

After running: 运行后:

df[['column']].fillna(value=myValue, inplace=True)

or: 要么:

df['column'].fillna(value=myValue, inplace=True)

or: 要么:

# Throws warning "A value is trying to be set on a copy of a slice..."
df.fillna({'column': myValue}, inplace=True)

or: 要么:

df[['column']] = df[['column']].fillna({'column': myValue})

or: 要么:

df['column'] = df['column'].fillna({'column': myValue})

My df['column'] still contains nan (!) 我的df['column']仍然包含nan (!)

list(df['column'].unique()) returns ['a', 'b', 'c', 'd', nan] and sum(pd.isnull(df['column'])) returns 1,000+. list(df['column'].unique())返回['a', 'b', 'c', 'd', nan]并且sum(pd.isnull(df['column']))返回1,000 +。

I've tried several variations but this problem persists. 我尝试了几种变体,但此问题仍然存在。 How do you fillna in place on a column in pandas? 您如何在大熊猫的某列上填充?

Ed Chum's comment's correctly points out the difference between the methods you propoosed. Ed Chum的评论正确地指出了您所建议的方法之间的差异。 Here is an example I used to show how it works. 这是我用来显示其工作原理的示例。

import pandas as pd
import numpy as np

d = {'col1': [1, 2, 3, 4], 'col2': [3, 4, np.nan, np.nan]}
df = pd.DataFrame(data=d)

df
   col1  col2
0     1   3.0
1     2   4.0
2     3   NaN
3     4   NaN
df['col2'].fillna(value=6, inplace=True)
   col1  col2
0     1   3.0
1     2   4.0
2     3   6.0
3     4   6.0

Having posted this, I think it'd be most valuable to see what your my_value variable's value is and what your dataframe looks like. 发布此内容后,我认为查看my_value变量的值和数据my_value的外观是最有价值的。

I discard Aditya's hypothesis. 我放弃了Aditya的假设。 In the case the nan would be a string, it would appear between quotations marks, and it doesn't. 如果nan是一个字符串,它会出现在引号之间,而不会出现。

Hope this helps! 希望这可以帮助!

One cause of this problem can be that the nan values in your dataset might be the string 'nan' instead of NaN. 造成此问题的一个原因可能是数据集中的nan值可能是字符串“ nan”而不是NaN。 To solve this, you can use the replace() method instead of fillna(). 要解决此问题,可以使用replace()方法代替fillna()。

Eg code: 例如代码:

df['column'].replace(to_replace='nan',value=myValue,inplace=True)

First of all, the correct syntax from your list is 首先,您列表中的正确语法是

df['column'].fillna(value=myValue, inplace=True)

If list(df['column'].unique()) returns ['a', 'b', 'c', 'd', nan] , this means that the values in your dataset are probably not equal to np.NaN , but rather equal to the string "nan". 如果list(df['column'].unique())返回['a', 'b', 'c', 'd', nan] ,则意味着数据集中的值可能不等于np.NaN ,但等于字符串“ nan”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM