我曾经使用较旧的 Pandas 版本(0.22,py2)以特定方式转置表格。最近,我有一些时间开始将整个脚本移动到 Pandas 2 / py3,我注意到它在
df.replace()
停止工作,抛出 ValueError
输入
df
看起来像这样:
Fruit Vegetable Sour_taste
Apple 1.0 NaN 1.0
Lemon 1.0 NaN 1.0
Potato NaN 1.0 NaN
我希望它看起来像这样:
Apple Fruit,Sour_taste
Lemon Fruit,Sour_taste
Potato Vegetable
我曾经在 pandas 0.22/py2 中像这样转换这个表:
df = df.apply(lambda column: column.replace(to_replace = column.loc[column > 0], value = column.name), axis = 0)
df["merged"] = df.apply(lambda row: ",".join(row.dropna().astype(str)), axis=1)
df = df[["merged"]].reset_index()
但是 pandas2/py3 中的第一行代码结果是:
ValueError: Series.replace cannot use dict-like to_replace and non-None value
我既不理解错误消息,也不理解为什么会发生。
在 pandas 的最新版本中,当您在案例中使用类似字典/系列的值 (column[column > 1]) 时,您不应显式指定替换值
value
,因为替换值会自动从字典中获取每个对应的键。话虽如此,您可以使用点积进行反向编码来简化代码,使其更加高效
(df.ge(1) @ ( df.columns + ', ')).str[:-2]
Apple Fruit, Sour_taste
Lemon Fruit, Sour_taste
Potato Vegetable
dtype: object
要修复代码,您需要屏蔽这些值,然后使用
apply
中的索引:
df.where(df.ge(1)).apply(lambda x: ','.join(x.dropna().index), axis=1)
输出:
Apple Fruit,Sour_taste
Lemon Fruit,Sour_taste
Potato Vegetable
dtype: object
或者,如果您有包含许多行和列的稀疏输入,这可能会更有效:
out = (df
.where(df.ge(1)).stack().reset_index(1)['level_1']
.groupby(level=0).agg(','.join)
)