简体   繁体   English

Pandas 中的 Str.contain 用于所有值为 NaN 的列

[英]Str.contain in Pandas for a column with all values NaN

I'm going through a Dataframe generated from reading a PDF file.我正在查看通过读取 PDF 文件生成的 Dataframe。 When reading the file, it may happen that one of the columns contains only NaN values.读取文件时,其中一列可能仅包含 NaN 值。

I need to search for a string in all column, but running str.contains on the column with all NaN results in AttributeError: Can only use.str accessor with string values!我需要在所有列中搜索一个字符串,但是在所有 NaN 列上运行 str.contains 会导致AttributeError: Can only use.str accessor with string values!

The code below results in the error.下面的代码导致错误。 Change one of the NaN values to a string though, and it works.不过,将其中一个 NaN 值更改为字符串,它就可以工作。 How do I get around this?我该如何解决这个问题?

d = {'col': [np.nan, np.nan, np.nan, np.nan, np.nan]}
df = pd.DataFrame(data=d)
df['col'].str.contains('Total Due This Invoice - USD', na=False)

One idea is replace missing values to some non match string value, eg here empty string:一个想法是将缺失值替换为一些不匹配的字符串值,例如这里的空字符串:

m = df['col'].fillna('').str.contains('Total Due This Invoice - USD')
print (m)
0    False
1    False
2    False
3    False
4    False
Name: col, dtype: bool

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

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