将整数映射到 Pandas 列中的特定字符串

Sau*_*aud 0 python pandas

pandas 中的数据框有两列“ text ”、“ Condition ”。在“条件”列中,它包含每行“文本”的数值。值为 1、-1、0。我想将这些整数值转换为文本标签,例如1 表示正数,-1 表示负数,0 表示中性。我怎样才能做到这一点?

数据框看起来像:

Moh*_*ani 8

如果我正确理解你的问题,你可以通过以下方式改变你的价值观。

使用Series.map

df['condition'] = df['condition'].map({1:'positive', -1:'negative', 0:'neutral'})
Run Code Online (Sandbox Code Playgroud)

使用Series.replace

df['condition'] = df['condition'].replace({1:'positive', -1:'negative', 0:'neutral'}}
Run Code Online (Sandbox Code Playgroud)