[英]Slice a specific row out of pandas groupby dataframe
I need to slice a particular row out of a groupby dataframe using 2 columns titles (The image is provided).I need the output of "Age" for a given Gender and Country.我需要使用 2 列标题(提供图像)从 groupby dataframe 中切出特定行。对于给定的性别和国家,我需要“年龄”的 output。
Now that you didnt give data.既然你没有提供数据。 Lets use free data from seaborn
让我们使用来自 seaborn 的免费数据
Data数据
import pandas as pd
import seaborn as sns
sns.set()
tips = sns.load_dataset("tips")
tips
Groupby to get similar result to yours Groupby 获得与您相似的结果
df=tips.groupby(['sex','time'])['tip'].sum().to_frame()
df
Several ways to slice multi index for row;为行分割多索引的几种方法;
##Silce Male Dinner
df.loc['Male','Dinner',:]
##Silce Female Dinner and Lunch
df.loc['Female',:]
#slice(None) to select all the contents of that level.
df.loc[(slice(None),'Lunch'),:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.