如何从多索引数据帧中选择特定列?

2024-04-26 23:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

播放kaggle啤酒评论数据集

https://www.kaggle.com/rdoume/beerreviews

df.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 1504037 entries, 1586613 to 39648
Data columns (total 13 columns):
brewery_id            1504037 non-null int64
brewery_name          1504037 non-null object
review_time           1504037 non-null int64
review_overall        1504037 non-null float64
review_aroma          1504037 non-null float64
review_appearance     1504037 non-null float64
review_profilename    1504037 non-null object
beer_style            1504037 non-null object
review_palate         1504037 non-null float64
review_taste          1504037 non-null float64
beer_name             1504037 non-null object
beer_abv              1504037 non-null float64
beer_beerid           1504037 non-null int64
dtypes: float64(6), int64(3), object(4)
memory usage: 160.6+ MB

我刚刚做了一个透视表,并返回以下结果

review_stat_by_beer = df[['beer_name','review_overall','review_aroma','review_appearance','review_palate','review_taste']]\
    .drop_duplicates(['beer_name'])\
    .pivot_table(index="beer_name", aggfunc=("count",'mean','median'))


review_stat_by_beer.info()

<class 'pandas.core.frame.DataFrame'>
Index: 44075 entries, ! (Old Ale) to 葉山ビール (Hayama Beer)
Data columns (total 15 columns):
(review_appearance, count)     44075 non-null int64
(review_appearance, mean)      44075 non-null float64
(review_appearance, median)    44075 non-null float64
(review_aroma, count)          44075 non-null int64
(review_aroma, mean)           44075 non-null float64
(review_aroma, median)         44075 non-null float64
(review_overall, count)        44075 non-null int64
(review_overall, mean)         44075 non-null float64
(review_overall, median)       44075 non-null float64
(review_palate, count)         44075 non-null int64
(review_palate, mean)          44075 non-null float64
(review_palate, median)        44075 non-null float64
(review_taste, count)          44075 non-null int64
(review_taste, mean)           44075 non-null float64
(review_taste, median)         44075 non-null float64
dtypes: float64(10), int64(5)
memory usage: 5.4+ MB

正在尝试选择这些列

review_stat_by_beer.(review_appearance, count)  # SyntaxError: invalid syntax

review_stat_by_beer[(review_appearance, count)] #NameError: name 'review_appearance' is not defined

review_stat_by_beer['(review_appearance, count)'] #KeyError: '(review_appearance, count)'

如何选择这些数据透视表结果?我的最终目标是在两列之间进行计算:

(review_overall, mean) minus (review_taste, mean)

有什么想法吗?谢谢


Tags: nameobjectcountmeannullreviewmediannon
1条回答
网友
1楼 · 发布于 2024-04-26 23:19:05

有几个选项可用于从多索引中选择特定结果:

# Setup
df =  pd.DataFrame(np.arange(9).reshape(3, 3))
df.columns = [['A', 'A', 'B'], ['a', 'b', 'c']]
df

   A     B
   a  b  c
0  0  1  2
1  3  4  5
2  6  7  8 

直接选择

df[('A', 'a')]

0    0
1    3
2    6
Name: (A, a), dtype: int64

通过loc

df.loc[:, ('A', 'a')]
# or 
# df.loc(axis=1)[('A', 'a')]  

0    0
1    3
2    6
Name: (A, a), dtype: int64

还有{}

df.xs(('A', 'a'), axis=1)

0    0
1    3
2    6
Name: (A, a), dtype: int64

所有这些情况下的想法都是传递一个字符串元组,分别表示第一级和第二级(列索引有两级)。在你的情况下,这看起来像

review_stat_by_beer[('review_appearance', 'count')]

有更多的方法,但这些是最好的

相关问题 更多 >