简体   繁体   English

在熊猫中填写缺失值错误?

[英]Fill missing values error in pandas?

I have a dataframe with quite a few URLs. 我有一个带有很多URL的数据框。 However, some are missing. 但是,有些丢失了。 It basically looks like this: 基本上看起来像这样:

import pandas as pd
import numpy as np
csv = [{"url_1" : np.NaN, "url_2" : "https://www.mercedes-benz.de/content/germany/mpc/mpc_germany_website/de/home_mpc/passengercars/home/new_cars/models/mercedes_amg_gt/r190.html"}]

df = pd.DataFrame(csv)

In this case, url_1 is missing. 在这种情况下,缺少url_1 I am trying to replace it with the entries in column url_2 . 我正在尝试将其替换为url_2列中的条目。 This is what I do: 这是我的工作:

df.url_1 = df.url_1.fillna(df.url_2, inplace=True)

This is the result: 结果如下:

    url_1   url_2
0   None    https://www.mercedes-benz.de/content/germany/m...

I have two questions: 我有两个问题:

(1) Why isn't the missing value replaced? (1)为什么不替换缺失值?

(2) In the original data set I am thrown an error: invalid fill value with a <class 'pandas.core.frame.DataFrame'> The dataframe looks exactly the same - and I at least do not get an error in the small test I presented above. (2)在原始数据集中,我抛出了一个错误: invalid fill value with a <class 'pandas.core.frame.DataFrame'>invalid fill value with a <class 'pandas.core.frame.DataFrame'>数据invalid fill value with a <class 'pandas.core.frame.DataFrame'>看起来完全相同-而且我至少在小范围内没有得到错误我上面提出的测试。 What does the error tell me and how do I get rid of it? 该错误告诉我什么?如何消除该错误?

Any help is gladly appreciated! 任何帮助都将不胜感激! Thank you, /R 谢谢/ R

You need remove inplace if want assign output, because if inplace parameter function return None : 如果要分配输出,则需要删除inplace ,因为inplace参数函数返回None

df.url_1 = df.url_1.fillna(df.url_2)
print (df)
                                               url_1  \
0  https://www.mercedes-benz.de/content/germany/m...   

                                               url_2  
0  https://www.mercedes-benz.de/content/germany/m...  

print (df.url_1.fillna(df.url_2, inplace=True))
None

Or dont assign and use inplace : 或不要inplace分配和使用:

df.url_1.fillna(df.url_2, inplace=True)
print (df)
                                               url_1  \
0  https://www.mercedes-benz.de/content/germany/m...   

                                               url_2  
0  https://www.mercedes-benz.de/content/germany/m...  

(1) & (2) (1)和(2)

You can't use assignation and the keyword inplace 您不能使用分配和关键字就地

df['url_1'] = df['url_1'].fillna(df['url_2'])
# or
df['url_1'].fillna(df['url_2'], inplace=True)

This should solve both problems. 这应该解决两个问题。

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

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