Python 使用group by后,如何将结果保存为数据帧

Python 使用group by后,如何将结果保存为数据帧,python,pandas,group-by,Python,Pandas,Group By,我试过这个: df1 =pd.DataFrame({'tpye':['continous','continous','continous','categorical','categorical','categorical'],'variable':['sales_a','sales_c','sales_b','card','gender','age']}) 如何使用pd.groupby从df中获取df1?看起来您只需要根据列type对值进行排序,请尝试df1=df.sort\u values(

我试过这个:

df1 =pd.DataFrame({'tpye':['continous','continous','continous','categorical','categorical','categorical'],'variable':['sales_a','sales_c','sales_b','card','gender','age']})

如何使用pd.groupby从df中获取df1?

看起来您只需要根据列
type
对值进行排序,请尝试
df1=df.sort\u values('type')
,如果没有,请解释您试图做什么
df.sort\u values('type',升序=False)
谢谢,sort\u值比groupby好得多。@ansev anky 91
df1 =pd.DataFrame({'tpye':['continous','continous','continous','categorical','categorical','categorical'],'variable':['sales_a','sales_c','sales_b','card','gender','age']})
df1 = pd.DataFrame(df.groupby('type'))