python pandas operations on columns

前端 未结 4 430
北海茫月
北海茫月 2021-02-06 11:52

Hi I would like to know the best way to do operations on columns in python using pandas.

I have a classical database which I have loaded as a dataframe, and I often have

4条回答
  •  遇见更好的自我
    2021-02-06 12:41

    There's lots of ways of doing this, but here's the pattern I find easiest to read.

    #Assume df is a Panda's dataframe object
    idx = df.loc[:, 'A'] > x
    df.loc[idx, 'A'] = df.loc[idx, 'C'] - df.loc[idx, 'D']
    

    Setting the elements less than x is as easy as df.loc[~idx, 'A'] = 0

提交回复
热议问题