且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pandas 取代价值观

更新时间:2023-02-27 10:32:34

只要您对pandas允许的df.loc[condition, column]语法感到满意,这很容易,只需执行df['col'] != 'pre'即可找到所有应进行更改:

As long as you're comfortable with the df.loc[condition, column] syntax that pandas allows, this is very easy, just do df['col'] != 'pre' to find all rows that should be changed:

df['col2'] = df['col']
df.loc[df['col'] != 'pre', 'col2'] = 'nonpre'

df
Out[7]: 
    col    col2
0   pre     pre
1  post  nonpre
2     a  nonpre
3     b  nonpre
4  post  nonpre
5   pre     pre
6   pre     pre