spl*_*ter 4 series missing-data python-3.x pandas fillna
给定一个pd.Series,我想用一个列表替换空值。也就是说,给定:
import numpy as np
import pandas as pd
ser = pd.Series([0,1,np.nan])
Run Code Online (Sandbox Code Playgroud)
我想要一个可以返回的函数
0 0
1 1
2 [nan]
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用自然函数,即fillna:
result = ser.fillna([np.nan])
Run Code Online (Sandbox Code Playgroud)
但我得到了错误
TypeError:“值”参数必须是标量或字典,但您传递了“列表”
有什么简单的方法可以达到这个目的吗?
使用apply,因为fillna仅使用标量:
print (ser.apply(lambda x: [np.nan] if pd.isnull(x) else x))
0 0
1 1
2 [nan]
dtype: object
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1330 次 |
| 最近记录: |