import pandas as pd
import numpy as np
d = {'age' : [21, 45, 45, 5],
'salary' : [20, 40, 10, 100]}
df = pd.DataFrame(d)
Und möchte eine zusätzliche Spalte namens "is_rich" hinzufügen, die abhängig von ihrem Gehalt reich ist, wenn eine Person reich ist. Ich habe mehrere Möglichkeiten gefunden, dies zu erreichen: < /p>
Code: Select all
# method 1
df['is_rich_method1'] = np.where(df['salary']>=50, 'yes', 'no')
# method 2
df['is_rich_method2'] = ['yes' if x >= 50 else 'no' for x in df['salary']]
# method 3
df['is_rich_method3'] = 'no'
df.loc[df['salary'] > 50,'is_rich_method3'] = 'yes'