Pandas Statistiche e Raggruppamenti
Pandas fornisce strumenti per statistiche e raggruppamenti tra data frame
Pandas Statistiche di base
import pandas as pd
# Creare un DataFrame di esempio
data = {
'Nome': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'Età': [25, 30, 35, 40, 45],
'Salario': [50000, 54000, 58000, 62000, 66000]
}
df = pd.DataFrame(data)
Statistiche descrittive
# Statistiche descrittive di base
statistiche = df.describe()
print(statistiche)
Raggruppare e calcolare la media
# Creare un DataFrame di esempio
data = {
'Nome': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'Età': [25, 30, 35, 40, 45],
'Salario': [50000, 54000, 58000, 62000, 66000],
'Dipartimento': ['HR', 'IT', 'IT', 'HR', 'Finance']
}
df = pd.DataFrame(data)
# Raggruppare per Dipartimento e calcolare la media del salario
media_salario = df.groupby('Dipartimento')['Salario'].mean()
print(media_salario)
Raggruppare e calcolare più aggregati
# Raggruppare per Dipartimento e calcolare la media e il massimo del salario
aggregati_salario = df.groupby('Dipartimento')['Salario'].agg(['mean', 'max'])
print(aggregati_salario)
Join tra DataFrame
# Creare due DataFrame di esempio
data1 = {
'ID': [1, 2, 3],
'Nome': ['Alice', 'Bob', 'Charlie']
}
df1 = pd.DataFrame(data1)
data2 = {
'ID': [1, 2, 4],
'Salario': [50000, 54000, 60000]
}
df2 = pd.DataFrame(data2)
Inner Join
# Inner join
df_inner = pd.merge(df1, df2, on='ID', how='inner')
print(df_inner)
Left Join
# Left join
df_left = pd.merge(df1, df2, on='ID', how='left')
print(df_left)
Right Join
# Right join
df_right = pd.merge(df1, df2, on='ID', how='right')
print(df_right)
Outer Join
# Outer join
df_outer = pd.merge(df1, df2, on='ID', how='outer')
print(df_outer)
Esempio Completo
import pandas as pd
# Creare un DataFrame di esempio
data = {
'Nome': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
'Età': [25, 30, 35, 40, 45],
'Salario': [50000, 54000, 58000, 62000, 66000],
'Dipartimento': ['HR', 'IT', 'IT', 'HR', 'Finance']
}
df = pd.DataFrame(data)
# Calcolare statistiche descrittive
statistiche = df.describe()
print("Statistiche descrittive:")
print(statistiche)
# Raggruppare per Dipartimento e calcolare la media del salario
media_salario = df.groupby('Dipartimento')['Salario'].mean()
print("\nMedia del salario per Dipartimento:")
print(media_salario)
# Raggruppare per Dipartimento e calcolare la media e il massimo del salario
aggregati_salario = df.groupby('Dipartimento')['Salario'].agg(['mean', 'max'])
print("\nAggregati del salario per Dipartimento (media e massimo):")
print(aggregati_salario)
# Creare due DataFrame per dimostrare i join
data1 = {
'ID': [1, 2, 3],
'Nome': ['Alice', 'Bob', 'Charlie']
}
df1 = pd.DataFrame(data1)
data2 = {
'ID': [1, 2, 4],
'Salario': [50000, 54000, 60000]
}
df2 = pd.DataFrame(data2)
# Eseguire vari tipi di join
df_inner = pd.merge(df1, df2, on='ID', how='inner')
df_left = pd.merge(df1, df2, on='ID', how='left')
df_right = pd.merge(df1, df2, on='ID', how='right')
df_outer = pd.merge(df1, df2, on='ID', how='outer')
print("\nInner join:")
print(df_inner)
print("\nLeft join:")
print(df_left)
print("\nRight join:")
print(df_right)
print("\nOuter join:")
print(df_outer)