如何从pandas DataFrame中提取一个子列? [英] How to extract a sub-column from a pandas DataFrame?

查看:86
本文介绍了如何从pandas DataFrame中提取一个子列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三列的表: A,B C .每列进一步分为两个子列:名称规则.

我需要使用 matplotlib 在 Name 子列中绘制三个饼图,但我不知道如何提取子列.这是我尝试过的方法,但是没有用:

chart = df['A'['Name']].value_counts().plot(kind='pie', labels=labels, autopct='%1.1f%%')

解决方案

您可能想阅读Multiindexing和

I have a table with three columns: A, B and C. Each column is further divided into two sub-columns: Name and Rule.

I need to plot three pie charts out the Name sub-columns using matplotlib, but I don't know how to extract the sub-column. This is what I tried, but it did not work:

chart = df['A'['Name']].value_counts().plot(kind='pie', labels=labels, autopct='%1.1f%%')

解决方案

You might want to read on Multiindexing and Slicing.

import pandas as pd
import numpy as np

arrays = [['A', 'A', 'B', 'B', 'C', 'C'],
          ['Name', 'Rule', 'Name', 'Rule', 'Name', 'Rule']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.DataFrame(np.random.rand(3, 6)*10, columns=index)

#creates this dataframe:
#first          A                   B                   C          
#second      Name      Rule      Name      Rule      Name      Rule
#0       2.075001  4.702192  3.480122  1.785579  5.078655  9.053004
#1       7.313122  3.762273  7.423559  8.713660  9.107358  5.643705
#2       8.981356  9.748874  1.131691  1.487273  0.096690  6.175825

# then index it with a none slice for the first column index and `"Name"` for the second.

df.loc[:,(slice(None), 'Name')].plot(kind='pie', subplots=True, autopct='%1.1f%%')

这篇关于如何从pandas DataFrame中提取一个子列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆