Nkango
Nkango

Reputation: 31

Create a new column in a dataframe pandas

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

Upvotes: 1

Views: 297

Answers (4)

Rodalm
Rodalm

Reputation: 5433

Although I think @mozway's solution is the cleanest one, here is another way using numpy.select

import numpy as np

df['new_column'] = np.select([df['selling_price'] < 50_000, 
                              df['selling_price'] <= 100_000], 
                             ['Cheap', 'Fair'], 'Expensive')

Upvotes: 2

mozway
mozway

Reputation: 260335

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'])

Upvotes: 1

Ao Sun
Ao Sun

Reputation: 76

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,
)

Upvotes: 1

Daniel Weigel
Daniel Weigel

Reputation: 1137

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

Upvotes: 0

Related Questions