python - 无法用 DataFrame 上的方法填充替换 ['' ]

标签 python pandas

运行python2.7

问题1

我想在我的数据框“test”中用 None 替换空字符串“”:

    from numpy.random import randn
    test = pd.DataFrame(randn(3,2))
    test.iloc[0,0]=''
    test.replace('', None)

给我错误类型错误:

不能用 DataFrame 上的方法填充替换 ['']。

出了什么问题?

问题二:

从 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

我很期待

test 
          0         1
0    None           b
1  0.042052  -1.44156
2  0.462131 -0.303288

最佳答案

None 被解释为缺乏论据。尝试:

test.replace({'':None})

问题二:

test.where(test != '', None)

关于python - 无法用 DataFrame 上的方法填充替换 ['' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51109471/

相关文章:

python - 如何为python指定 native 库搜索路径

python - 如何在python中调整直方图上的字体大小

python - Python中特定类名的列表

php - Python 应用程序向服务器发送 get 链接,得到 200 OK 响应,但服务器脚本不执行

Python Pandas : Table manipulation with slices as variables

python - Count 满足条件的序列总数,无 for 循环

python - 如何对多个数据帧重新采样?

python - Pandas:获取列中有效后续帧的第一个和最后一个索引

python - 将通过网络收到的 python 字符串 (FIX) 解释为二进制

Python - 越来越快地运行程序