Pandas generates new columns through LOC

A very convenient way to use pandas is through indexes such as LOC, iloc and IX. here is a record:

df.loc [condition, new column] = assign initial value

If the new column name is an existing column name, it will be changed on the original data column

import pandas as pd
import numpy as np
 
data = pd.DataFrame ( np.random.randint (0,100,40).reshape(10,4),columns=list(‘abcd’))
print(data)
data.loc [data. D & gt; = 50, ‘greater than 50′] =’Yes’
Print (data)

By using LOC to index, judge in the index, and then assign value to the new column according to the result of judgment. This is a very convenient and basic operation. Of course, I don’t remember it clearly recently, so I’ll record it here.

 

Read More: