[英]How to select columns with a MultiIndex in a pandas DataFrame (for seaborn scatter plot)?
I have an aggregated DataFrame, with nested columns.我有一个带有嵌套列的聚合 DataFrame。 How can I retrieve the aggregated columns,
sum
and count
, as the x and y of a scatter plot?如何检索聚合列
sum
和count
作为散点 plot 的 x 和 y?
I tried to use我试着用
sns.scatterplot(x="sum", y="count", data=xyz)
but it gave me keyError"sum"
.但它给了我
keyError"sum"
。
The only way it works is to retrieve sum
and count
as two lists, then scatterplot these two lists.它起作用的唯一方法是将
sum
和count
作为两个列表检索,然后对这两个列表进行散点图。 But it takes a long time.但这需要很长时间。
Is there any simple way to retrieve these two nested columns straight away?有什么简单的方法可以立即检索这两个嵌套列吗?
Thank you.谢谢你。
From your picture it looks like you have a MultiIndex for the columns.从您的图片看来,您的列有一个 MultiIndex。 If that's the case, you can access the columns by indexing with a tuple that specifies the values for each level:
如果是这种情况,您可以通过使用指定每个级别的值的元组进行索引来访问列:
sns.scatterplot(df[('Total', 'sum')], df[('Total', 'count')])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.