import pandas as pd
dic={'省份':['A','A','A','B','C'],'城市':['a','a','a_','b','c',],'订单额':[1,2,3,4,5],'规模':[5,4,3,2,1]}
df=pd.DataFrame(dic)
# print(df)
df1=df.groupby('城市')['订单额'].sum()
df2=df.groupby(['省份','城市'])[['订单额','规模']].sum()
df3=df.groupby(['城市'])[['订单额']].sum()
print(df1['a'])#实际上是一对一的映射关系
print(df2.loc['A','a']['订单额'])
#df2.loc['a']['订单额']报错:此对象不存在名为的'a'索引,可通过set_index解决
#df2.loc['A']['订单额']是一组只有A省份的城市的'订单额'
print(df3.loc['a']['订单额'])
print(list(df1))
print(list(df2['订单额']))
print(list(df3['订单额']))
#set_index用法
df2=df2.reset_index()#重置行索引,把原来的行索引内容变为列【此处是新增'省份'和'城市'两列数据】,行索引变为0,1,2...
df2=df2.set_index('城市')#设置行索引为'城市'
#此时的df2相当于是df3多加了'省份'这一列
print(df2)
import pandas as pd
dic={'省份':['A','A','A','B','C'],'城市':['a','a','a_','b','c',],'订单额':[1,2,3,4,5],'规模':[5,4,3,2,1]}
df=pd.DataFrame(dic)
# print(df)
df1=df.groupby('城市')['订单额'].sum()
df2=df.groupby(['省份','城市'])[['订单额','规模']].sum()
df3=df.groupby(['城市'])[['订单额']].sum()
# 行索引
print(df1.index)
print(df2.index)
print(df3.index)
#单个值
print(df1['a'])#实际上是一对一的映射关系
print(df2.loc['A','a']['订单额'])
#df2.loc['a']['订单额']报错:此对象不存在名为的'a'索引,可通过set_index解决
#df2.loc['A']['订单额']是一组只有A省份的城市的'订单额'
print(df3.loc['a']['订单额'])
#一整列
print(list(df1))
print(list(df2['订单额']))
print(list(df3['订单额']))
#set_index用法
df2=df2.reset_index()#重置行索引,把原来的行索引内容变为列【此处是新增'省份'和'城市'两列数据】,行索引变为0,1,2...
df2=df2.set_index('城市')#设置行索引为'城市'
#此时的df2相当于是df3多加了'省份'这一列
print(df2)
import pandas as pd
dic={'省份':['A','A','A','B','C'],'城市':['a','a','a_','b','c',],'订单额':[1,2,3,4,5],'规模':[5,4,3,2,1]}
df=pd.DataFrame(dic)
df_tmp=df[df['订单额'].between(1,2)]
df_tmp['订单额']=0
print(df_tmp)
df[df['订单额'].between(1,2)]=df_tmp
print(df)