“Pandy tworzą nową kolumnę warunkową na innych kolumnach” Kod odpowiedzi

Wykonaj instrukcję warunku na pand kolumnowych

df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
Fragile Finch

Pandy tworzą nową kolumnę warunkową na innych kolumnach

# For creating new column with multiple conditions
conditions = [
    (df['Base Column 1'] == 'A') & (df['Base Column 2'] == 'B'),
    (df['Base Column 3'] == 'C')]
choices = ['Conditional Value 1', 'Conditional Value 2']
df['New Column'] = np.select(conditions, choices, default='Conditional Value 1')
Talented Toucan

Pandy Utwórz nową kolumnę na podstawie warunku dwóch kolumn

conditions = [
    df['gender'].eq('male') & df['pet1'].eq(df['pet2']),
    df['gender'].eq('female') & df['pet1'].isin(['cat', 'dog'])
]

choices = [5,5]

df['points'] = np.select(conditions, choices, default=0)

print(df)
     gender      pet1      pet2  points
0      male       dog       dog       5
1      male       cat       cat       5
2      male       dog       cat       0
3    female       cat  squirrel       5
4    female       dog       dog       5
5    female  squirrel       cat       0
6  squirrel       dog       cat       0
Courageous Cobra

Odpowiedzi podobne do “Pandy tworzą nową kolumnę warunkową na innych kolumnach”

Pytania podobne do “Pandy tworzą nową kolumnę warunkową na innych kolumnach”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu