私信  •  关注

wordsforthewise

wordsforthewise 最近创建的主题
wordsforthewise 最近回复了
5 年前
回复了 wordsforthewise 创建的主题 » 如何在pandas python中为字符串创建汇总列[duplicate]

DSM已经得到了公认的答案,但是编码似乎并不适合所有人。以下是一款适用于当前版本的熊猫(截至2018年8月,0.23.4):

import pandas as pd

df = pd.DataFrame({'col1': [1, 2, 2, 3, 1],
            'col2': ['negative', 'positive', 'neutral', 'neutral', 'positive']})

conversion_dict = {'negative': -1, 'neutral': 0, 'positive': 1}
df['converted_column'] = df['col2'].replace(conversion_dict)

print(df.head())

你会看到它看起来像:

   col1      col2  converted_column
0     1  negative                -1
1     2  positive                 1
2     2   neutral                 0
3     3   neutral                 0
4     1  positive                 1

文档为 pandas.DataFrame.replace are here .