在 pandas 中用NaN替换空字符串 [英] replacing empty strings with NaN in Pandas
问题描述
我有一个pandas数据框(是通过导入csv文件创建的).我想用NaN替换空白值.这些空白值中有些是空的,有些包含(可变数量)空格''
,' '
,' '
等.
I have a pandas dataframe (that was created by importing a csv file). I want to replace blank values with NaN. Some of these blank values are empty and some contain a (variable number) of spaces ''
, ' '
, ' '
, etc.
使用此线程中的建议拥有
df.replace(r'\s+', np.nan, regex=True, inplace = True)
它确实替换了仅包含空格的所有字符串,但也替换了其中所有包含空格的字符串,这不是我想要的.
which does replace all the strings that only contain spaces, but also replaces every string that has a space in it, which is not what I want.
如何仅用 just 空格和空字符串替换字符串?
How do I replace only strings with just spaces and empty strings?
推荐答案
如果您正在读取csv
文件,并且想在读取文件时将所有空字符串都转换为nan
,那么您可以使用该选项
If you are reading a csv
file and want to convert all empty strings to nan
while reading the file itself then you can use the option
skipinitialspace=True
示例代码
pd.read_csv('Sample.csv', skipinitialspace=True)
这将删除定界符后出现的所有空白,因此将所有空字符串都设为nan
This will remove any white spaces that appear after the delimiters, Thus making all the empty strings as nan
从文档 http://pandas.pydata.org/pandas- docs/stable/io.html
注意: :此选项即使从有效数据中也会删除前一个空格,如果出于任何原因要保留前一个空格,则此选项不适合选择.
Note: This option will remove preceding white spaces even from valid data, if for any reason you want to retain the preceding white space then this option is not a good choice.
这篇关于在 pandas 中用NaN替换空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!