如何从pandas DataFrame中提取子列?

时间:2017-06-20 10:26:47

标签: python pandas matplotlib

我有一个包含三列的表: A,B C 。每列进一步分为两个子列: Name Rule

我需要使用matplotlib在 Name 子列中绘制三个饼图,但我不知道如何提取子列。这是我尝试过的,但它不起作用:

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

1 个答案:

答案 0 :(得分:3)

您可能希望阅读Multiindexing和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%%')

enter image description here