gpt4 book ai didi

python - 如何从多索引数据框中选择特定列?

转载 作者:行者123 更新时间:2023-12-04 10:11:35 24 4
gpt4 key购买 nike

播放 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)

有什么想法吗?谢谢!

最佳答案

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

# 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')]

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

关于python - 如何从多索引数据框中选择特定列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61313202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com