Home > front end >  How to crete a column with another existed column in python?
How to crete a column with another existed column in python?

Time:04-24

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?

error shows in this imagine

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)

CodePudding user response:

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)
  • Related