“pandas si sinon” Réponses codées

pandas si python

import pandas as pd

names = {'First_name': ['Jon','Bill','Maria','Emma']}
df = pd.DataFrame(names,columns=['First_name'])

df.loc[(df['First_name'] == 'Bill') | (df['First_name'] == 'Emma'), 'name_match'] = 'Match'  
df.loc[(df['First_name'] != 'Bill') & (df['First_name'] != 'Emma'), 'name_match'] = 'Mismatch'  

print (df)
maNu

pandas si python

df['new column name'] = df['column name'].apply(lambda x: 'value if condition is met' if x condition else 'value if condition is not met')
Calm Crab

Si la condition Dataframe Python

df.loc[df['age1'] - df['age2'] > 0, 'diff'] = df['age1'] - df['age2']
Glorious Goldfinch

pandas si sinon

df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met'
Fragile Finch

pandas si python

import pandas as pd

numbers = {'set_of_numbers': [1,2,3,4,5,6,7,8,9,10]}
df = pd.DataFrame(numbers,columns=['set_of_numbers'])

df['equal_or_lower_than_4?'] = df['set_of_numbers'].apply(lambda x: 'True' if x <= 4 else 'False')

print (df)
NA RACE

pandas si python

import pandas as pd

names = {'First_name': ['Jon','Bill','Maria','Emma']}
df = pd.DataFrame(names,columns=['First_name'])

df.loc[df['First_name'] == 'Bill', 'name_match'] = 'Match'  
df.loc[df['First_name'] != 'Bill', 'name_match'] = 'Mismatch'  
 
print (df)
maNu

Réponses similaires à “pandas si sinon”

Questions similaires à “pandas si sinon”

Plus de réponses similaires à “pandas si sinon” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code