python - 如何从 multiIndex 数据框中选择特定列?
问题描述
播放 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)
有什么想法吗?谢谢!
解决方案
有几个选项可用于从 multiIndex 中选择特定结果:
# 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
还有xs
,
df.xs(('A', 'a'), axis=1)
0 0
1 3
2 6
Name: (A, a), dtype: int64
在所有这些情况下的想法是传递一个字符串元组,分别表示第一级和第二级(您的列索引有 2 个级别)。在你的情况下,看起来像
review_stat_by_beer[('review_appearance', 'count')]
还有更多方法,但这些是最好的方法。
推荐阅读
- node.js - 如何将镶木地板文件上传到 nodejs 中的 s3 存储桶?
- ios - 防止自定义大小的 UITextView 增长超过某个点(例如键盘顶部锚点)
- javascript - 为什么经过身份验证的实时数据库写入会阻止在可公开写入的 Firebase 数据库中进行未经身份验证的写入?
- windows - 在 Windows 上模拟触摸屏“触摸”
- pipe - 使用python uno的libreoffice管道,计算机之间的连接不一致,有什么区别?
- java - 在 Android/Java 和 C# 中生成等效的 Hash 值
- android - 如何在 Kotlin 中使用 Retrofit 获取响应标头?
- tensorflow2.0 - 无法在 Tensorboard 中正确查看 Graph
- python - Django按python中的最新日期过滤
- javascript - jQuery Tablesorter reflow 在浏览器开发工具中看起来不错,但在智能手机上却不行