运行python2.7
问题1
我想在我的数据框“test”中用 None 替换空字符串 '' :
from numpy.random import randn
test = pd.DataFrame(randn(3,2))
test.iloc[0,0]=''
test.replace('', None)
Run Code Online (Sandbox Code Playgroud)
给我错误类型错误:
不能用 DataFrame 上的方法板替换 ['']。
什么地方出了错?
问题2:
从 numpy.random 导入 randn
test = pd.DataFrame(randn(3,2))
# this works
test.iloc[0,1]= 'A'
test[1] = test[1].replace('A','b')
# this does not
test.iloc[0,0]=''
test.loc[0] = test[0].replace('', None)
test
0 1
0 b
1 0.042052 -1.44156
2 0.462131 -0.303288
Run Code Online (Sandbox Code Playgroud)
我期待
test
0 1
0 None b
1 0.042052 -1.44156
2 0.462131 -0.303288
Run Code Online (Sandbox Code Playgroud)
没有一个被解释为缺乏论证。尝试:
test.replace({'':None})
Run Code Online (Sandbox Code Playgroud)
问题2:
test.where(test != '', None)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1604 次 |
| 最近记录: |