今天来练习pandas的使用

import pandas as pd
\'\'\'
s = pd.Series([100,200,300,400,500],index=[\'x\',\'y\',\'z\',\'w\',\'v\'])
print(s)

data = {
\'Name\':[\'John\', \'Jane\', \'Doe\'],
\'age\':[22, 28, 35],
\'city\':[\'Boston\', \'Chicago\', \'Miami\']
}
df = pd.DataFrame(data)
print(df)

print(df.head())
print(df.tail())
print(df.info())
print(df.describe(include=\'object\'))
filtered_df = df[df[\'age\']>25]
print(filtered_df)

df[\'salary\'] = [50000,60000,70000]
print(df)
df = df.drop(columns=[\'city\'])
print(df)

df = df[df[\'age\']>=28]
print(df)

df = df.drop(index=2)
print(df)
df.drop(df[df[\'age\']<30].index,inplace=True)
print(df)

data = {
\'Name\': [\'Alice\', \'Bob\', \'Charlie\', \'David\'],
\'age\': [25, 30, 35, 40],
\'city\': [\'NY\', \'LA\', \'NY\', \'LA\']
}
df = pd.DataFrame(data)
df[\'salary\'] = [50000,60000,70000,80000]
df = df.sort_values(by = \'age\',ascending=False)
grouped = df.groupby(\'city\')[\'age\'].mean()
print(df)
print(grouped)

data1 = {
\'ID\': [1, 2, 3],
\'Name\': [\'Alice\', \'Bob\', \'Charlie\'],
\'age\': [25, None, 35]
}
data2 = {
\'ID\': [2, 3, 4],
\'city\': [\'LA\', \'NY\', \'SF\'],
\'Score\': [80, 90, None]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

mergede_df = pd.merge(df1,df2,how=\'outer\')
print(mergede_df)

mergede_df[\'age\'] = mergede_df[\'age\'].fillna(mergede_df[\'age\'].mean())
mergede_df[\'Score\'] = mergede_df[\'Score\'].fillna(0)
ans_df = mergede_df[(mergede_df[\'age\']>30)&(mergede_df[\'city\']==\'NY\')]
print(mergede_df)
print(ans_df)

mergede_df = mergede_df.sort_values(by=\'Score\',ascending=False)
print(mergede_df)
grouped = mergede_df.groupby(\'city\')[\'age\'].mean()
print(grouped)
ans = mergede_df.pivot_table(values=\'Score\',index=\'city\',aggfunc=\'mean\')
print(ans)
\'\'\'

data = {
\'OrderID\': [1, 2, 3, 4, 5],
\'Customer\': [\'Alice\', \'Bob\', \'Charlie\', \'Alice\', \'Bob\'],
\'Category\': [\'Electronics\', \'Electronics\', \'Furniture\', \'Furniture\', \'Electronics\'],
\'Amount\': [250, 300, 150, 200, 400],
\'OrderDate\': [\'2023-01-15\', \'2023-02-20\', \'2023-01-20\', \'2023-02-25\', \'2023-01-30\']
}
df = pd.DataFrame(data)

df[\'OrderDate\'] = pd.to_datetime(df[\'OrderDate\'])
df.set_index(\'OrderDate\', inplace=True)
amount = df.groupby(\'Customer\')[\'Amount\'].sum()
print(amount)
sale_amount = df.resample(\'ME\').agg({
\'Amount\':\'mean\'
})
print(sale_amount)
rich = df[df[\'Amount\']>300]
print(rich)
pivot = df.pivot_table(values=\'Amount\', index=\'Customer\', columns=\'Category\', aggfunc=\'mean\').fillna(0)
print(pivot)

Python真简洁 之前java超级麻烦的而且跟Chat GPT学真爽还会出题目 讲的也不难学的真开心:)