熊猫在一条线上替换作品,但在另一行中则不然



我正在尝试清理一些数据(Python 3.7.3(,并且能够使用

def g(x):
return pd.Series(x.replace(np.nan, ""))
df = df.apply(g)

并获得所需的输出:

1    2      3            4
qt>  qml>   tableview>

等等,但后来我尝试做类似的事情,用空字符串替换">">

def h(x):
return pd.Series(x.replace(">", ""))
df = df.apply(h)

但是数据帧没有改变,我在每个单词的末尾仍然有">"。没有错误扔给我,所以我不知所措。提前感谢您的任何答案

h函数替换为:

def h(x):
return pd.Series(x.str.replace(">", ""))
df = df.apply(h)
def h(x):
return x.replace(">", "",regex=True)
df = df.apply(h)

请尝试以下操作:

def g(x):
return pd.Series(x.replace(">", "",regex=True))
df = df.apply(g)  

这应该有效。

最新更新