Python pandas - entering values ​​in a new column

I have a small information frame below the costs of 4 people. There is an empty column called Grade. I would like to appreciate those who spent more than $ 100 of Class A and Class B for those under $ 100. What is the most efficient way to populate the Grade column, assuming it's a large framework?

import pandas as pd df=pd.DataFrame({'Customer':['Bob','Ken','Steve','Joe'], 'Spending':[130,22,313,46]}) df['Grade']='' 

enter image description here

+1
source share
2 answers

You can use numpy.where :

 df['Grade']= np.where(df['Spending'] > 100 ,'A','B') print (df) Customer Spending Grade 0 Bob 130 A 1 Ken 22 B 2 Steve 313 A 3 Joe 46 B 

Delay

 df=pd.DataFrame({'Customer':['Bob','Ken','Steve','Joe'], 'Spending':[130,22,313,46]}) #[400000 rows x 4 columns] df = pd.concat([df]*100000).reset_index(drop=True) In [129]: %timeit df['Grade']= np.where(df['Spending'] > 100 ,'A','B') 10 loops, best of 3: 21.6 ms per loop In [130]: %timeit df['grade'] = df.apply(lambda row: 'A' if row['Spending'] > 100 else 'B', axis = 1) 1 loop, best of 3: 7.08 s per loop 
+1
source

The quickest way to do this is to use the lambda function with the apply function.

 df['grade'] = df.apply(lambda row: 'A' if row['Spending'] > 100 else 'B', axis = 1) 
+1
source

Source: https://habr.com/ru/post/1261340/


All Articles