简体   繁体   English

如何在 pandas DataFrame 中使用 MultiIndex 列 select 列(对于 ZD50F42A37901F21DE34BD 散点图)C?

[英]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?如何检索聚合列sumcount作为散点 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.它起作用的唯一方法是将sumcount作为两个列表检索,然后对这两个列表进行散点图。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM