Home > database >  Create a new column in a dataframe pandas
Create a new column in a dataframe pandas

Time:03-28

How do i create a new column in data frame that will say "Cheap" if the price is below 50000, "Fair" is the price is between 50000 and 100000 and "Expensive" if the price is over 100000enter image description here

CodePudding user response:

There are many options. A nice one is pandas.cut:

df['new'] = pd.cut(df['selling_price'],
                   bins=[0,50000,100000, float('inf')],
                   labels['cheap', 'fair', 'expensive'])

CodePudding user response:

You could use numpy.where() for this kind of data processing:

import numpy as np
df['Cheap']=np.where(df['selling_price']<=50000,'Cheap', #When selling_price <50k, 'Cheap', otherwise...
                     np.where((df['selling_price']>50000) & (df['selling_price']<100000) ,'Fair', #When selling_price >50k and <100k, 'Fair', otherwise...
                     np.where(df['selling_price']>=100000,'Expensive',#When selling_price >100k, Expensive
                    'N/A')))#Otherwise N/A - in case you have some string or other data type in your data

CodePudding user response:

Another way with apply() and lambda function :

df["new"] = df.selling_price.apply(
    lambda x: "cheap" if x < 50000 else 
        "fair" if x < 100000 else 
        "expensive"
)

Or in a general way which allows you to include multiple columns in the condition :

df["new"] = df.apply(
    lambda x: "cheap"
    if x["selling_price"] < 50000
    else "fair"
    if x["selling_price"] < 100000
    else "expensive",
    axis=1,
)
  • Related