简体   繁体   English

如何使用 pandas 擦除某个列中的“nan”值?

[英]How to erase 'nan' values that are in a certain column using pandas?

I have a column that has bunch of rows with a mixture of 'nan'.我有一列有一堆混合了“nan”的行。 I only want to delete 'nan', not the entire row that includes 'nan'.我只想删除“nan”,而不是包含“nan”的整行。 Some cells in that column have multiple nans like: nan,nan,nan,nan and some cells has the name that I need with nan attached like: Jefferson,nan,nan,nan该列中的某些单元格有多个 nan,例如:nan、nan、nan、nan,而某些单元格的名称是我需要的 nan,例如:Jefferson,nan,nan,nan

How can I just erase nan?我怎样才能抹去nan?

You can try to replace nan with some value, like 0 or ""?您可以尝试将 nan 替换为某个值,例如 0 或 ""? pandas.DataFrame.fillna pandas.DataFrame.fillna

From: geekforgeek来自: geekforgeek

Methods to replace NaN values with zeros in Pandas DataFrame:在 Pandas DataFrame 中用零替换 NaN 值的方法:

fillna()
The fillna() function is used to fill NA/NaN values using the specified method.
replace()
The dataframe.replace() function in Pandas can be defined as a simple method used to replace a string, regex, list, dictionary etc. in a DataFrame.

Steps to replace NaN values:替换 NaN 值的步骤:

For one column using pandas:

df['DataFrame Column'] = df['DataFrame Column'].fillna(0)

For one column using numpy:

df['DataFrame Column'] = df['DataFrame Column'].replace(np.nan, 0)

For the whole DataFrame using pandas:

df.fillna(0)

For the whole DataFrame using numpy:

df.replace(np.nan, 0)

MY ADVICE: as you use strings, replace NaN by a space " " and when processing your dataframe skip the value when == " "我的建议:当您使用字符串时,将 NaN 替换为空格“”,并在处理 dataframe 时跳过该值 ==“”

This should work.这应该有效。 I'm using regular expressions to match 'nan' only or 'nan,<something>' and I'm replacing that by an empty string '' .我正在使用正则表达式仅匹配'nan''nan,<something>'并且我将其替换为空字符串''

I decided to use regex because by your question I think you can't use a literal string since you don't know exactly is within the cell (can be any number of 'nan's .我决定使用正则表达式,因为根据您的问题,我认为您不能使用文字字符串,因为您不知道确切是在单元格内(可以是任意数量的'nan's .

import pandas as pd

data = {'names': ['Jefferson', 'nan', 'Olivia', 'nan', 'nan', 'nan,nan,nan', 'Rebekah'],
        'numbers': [1, 2, 3, 4, 5, 6, 7]}

df = pd.DataFrame(data=data)
df['names'] = df['names'].replace({r'^nan$': '', r'^nan,.*': ''}, regex=True)
df

If we are not talking about the string 'nan' then the df.fillna('') should do.如果我们不是在谈论字符串'nan'那么df.fillna('')应该可以。

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

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