简体   繁体   中英

How to crete a column with another existed column in python?

When I try to use apply() function to add the new column based on another column, there is an error. I do not what's going on here?

这个想象中的错误显示

carcrash_data = sns.load_dataset('car_crashes')
carcrash_df = pd.DataFrame(carcrash_data)
carcrash_df["ins_premium"].mean()
def categorise(carcrash_df):
    if carcrash_df['ins_premium'] > 886.9576470588235:
      return 'Above average'
    elif carcrash_df['ins_premium'] < 886.9576470588235:
      return 'Below average'
carcrash_df['premium']=carcrash_df.apply(lambda row:categorise(carcrash_df), axis=1)

In your apply function you take row , I believe you should pass it as an argument to your function as well. See the code below.

carcrash_data = sns.load_dataset('car_crashes')
carcrash_df = pd.DataFrame(carcrash_data)
carcrash_df["ins_premium"].mean()
def categorise(row):
    if row['ins_premium'] > 886.9576470588235:
      return 'Above average'
    elif row['ins_premium'] < 886.9576470588235:
      return 'Below average'
carcrash_df['premium']=carcrash_df.apply(lambda row:categorise(row), axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM