用 Pandas 中的空白或空字符串替换 NaN?
在本文中,我们将讨论如何在 Pandas 中将 NaN 替换为空白或空字符串。
创建具有 NaN 值的数据框以进行演示
为此,我们将使用 pandas dataframe() 对象创建数据框。
Python3
# import pandas module
import pandas as pd
# import numpy module
import numpy as np
# create dataframe with 3 columns
data = pd.DataFrame({
"name": ['sravan', np.nan, 'harsha', 'ramya'],
"subjects": [np.nan, 'java', np.nan, 'html/php'],
"marks": [98, np.nan, np.nan, np.nan]
})
# display
data
Python3
# import pandas module
import pandas as pd
# import numpy module
import numpy as np
# create dataframe with 3 columns
data = pd.DataFrame({
"name": ['sravan', np.nan, 'harsha', 'ramya'],
"subjects": [np.nan, 'java', np.nan, 'html/php'],
"marks": [98, np.nan, np.nan, np.nan]
})
# replace nan with empty string
# using replace() function
data.replace(np.nan, '')
Python3
# import pandas module
import pandas as pd
# import numpy module
import numpy as np
# create dataframe with 3 columns
data = pd.DataFrame({
"name": ['sravan', np.nan, 'harsha', 'ramya'],
"subjects": [np.nan, 'java', np.nan, 'html/php'],
"marks": [98, np.nan, np.nan, np.nan]
}
)
# replace nan with empty string
# using fillna() function
data[['name', 'subjects', 'marks']].fillna('')
Python3
# import pandas module
import pandas as pd
# import numpy module
import numpy as np
# create dataframe with 3 columns
data = pd.DataFrame({
"name": ['sravan', np.nan, 'harsha', 'ramya'],
"subjects": [np.nan, 'java', np.nan, 'html/php'],
"marks": [98, np.nan, np.nan, np.nan]
}
)
# replace nan with empty string
# using fillna() function
data.fillna('')
输出:
方法一:使用replace()函数
我们可以使用 replace()函数将 NaN 替换为空字符串。此函数将用空字符串替换 NaN 值
Syntax: dataframe.replace(np.nan, ”)
where
- dataframe is the input dataframe
- first parameter takes Nan value
- second parameter replace the NaN with empty string
例子:
Python3
# import pandas module
import pandas as pd
# import numpy module
import numpy as np
# create dataframe with 3 columns
data = pd.DataFrame({
"name": ['sravan', np.nan, 'harsha', 'ramya'],
"subjects": [np.nan, 'java', np.nan, 'html/php'],
"marks": [98, np.nan, np.nan, np.nan]
})
# replace nan with empty string
# using replace() function
data.replace(np.nan, '')
输出:
方法2:使用fillna()
这用于将多列 NaN 值替换为空字符串。
Syntax: dataframe[[‘columns’ ]].fillna(”)
where
- dataframe is the input dataframe
- columns are the multiple columns in the dataframe
示例:
Python3
# import pandas module
import pandas as pd
# import numpy module
import numpy as np
# create dataframe with 3 columns
data = pd.DataFrame({
"name": ['sravan', np.nan, 'harsha', 'ramya'],
"subjects": [np.nan, 'java', np.nan, 'html/php'],
"marks": [98, np.nan, np.nan, np.nan]
}
)
# replace nan with empty string
# using fillna() function
data[['name', 'subjects', 'marks']].fillna('')
输出:
我们也可以直接使用 fillna() 而不指定列:
dataframe.fillna('')
例子:
Python3
# import pandas module
import pandas as pd
# import numpy module
import numpy as np
# create dataframe with 3 columns
data = pd.DataFrame({
"name": ['sravan', np.nan, 'harsha', 'ramya'],
"subjects": [np.nan, 'java', np.nan, 'html/php'],
"marks": [98, np.nan, np.nan, np.nan]
}
)
# replace nan with empty string
# using fillna() function
data.fillna('')
输出: