gpt4 book ai didi

python - 值错误 : Grouper for not 1-dimensional

转载 作者:IT老高 更新时间:2023-10-28 20:30:56 27 4
gpt4 key购买 nike

我有以下代码通过 seaborn 创建一个表格和一个条形图。

#Building a dataframe grouped by the # of Engagement Types
sales_type = sales.groupby('# of Engagement Types').sum()

#Calculating the % of people who bought the course by # engagement types
sales_type['% Sales per Participants'] = round(100*(sales_type['Sales'] / sales_type['Had an Engagement']), 2)

#Calculating the # of people who didn't have any engagements
sales_type.set_value(index=0, col='Had an Engagement', value=sales[sales['Had an Engagement']==0].count()['Sales'])

#Calculating the % of sales for those who didn't have any engagements
sales_type.set_value(index=0, col='% Sales per Participants',
value=round(100 * (sales_type.ix[0, 'Sales'] /
sales[sales['Had an Engagement']==0].count()['Sales']),2))

#Setting the graph image
fig, (ax1) = plt.subplots(nrows=1, ncols=1, figsize=(12,4))
sns.set_style("whitegrid")

# Ploting the histagram for the % of total prospects
ax1 = sns.barplot(x=sales_type.index,y='% Sales per Participants', data=sales_type ,ax=ax1)
ax1.set(ylabel = '%')
ax1.set_title('% Sales per Participants By # of Engagement Types')

#present the table
sales_type.xs(['Had an Engagement', 'Sales','% Sales per Participants'],axis=1).transpose()
#sales_type

我对其他没有问题的参数使用相同的代码概念。但是,对于一个参数,我收到一个错误:行代码的“ValueError: Grouper for '' not 1-dimensional”:

ax1 = sns.barplot(x=sales_type.index,y='% Sales per Participants', data=sales_type ,ax=ax1)

尽管数据框的维度不超过一维,但仍会发生此错误。

这是 table 的头:

                       Sales  Pre-Ordered / Ordered Book  \
# of Engagement Types
0 1.0 0.0
1 20.0 496.0
2 51.0 434.0
3 82.0 248.0
4 71.0 153.0
5 49.0 97.0
6 5.0 24.0

Opted In For / Clicked to Kindle Viewed PLC \
# of Engagement Types
0 0.0 0
1 27034.0 5920
2 6953.0 6022
3 1990.0 1958
4 714.0 746
5 196.0 204
6 24.0 24

# of PLC Engagement Viewed Webinar \
# of Engagement Types
0 0.0 0
1 6434.0 1484
2 7469.0 1521
3 2940.0 1450
4 1381.0 724
5 463.0 198
6 54.0 24

# of Webinars (Live/Replay) \
# of Engagement Types
0 0.0
1 1613.0
2 1730.0
3 1768.0
4 1018.0
5 355.0
6 45.0

OCCC Facebook Group Member Engaged in Cart-Open \
# of Engagement Types
0 0.0 0
1 148.0 160
2 498.0 1206
3 443.0 967
4 356.0 511
5 168.0 177
6 24.0 24

# of Engagement at Cart Open Had an Engagement \
# of Engagement Types
0 0.0 3387
1 189.0 35242
2 1398.0 8317
3 1192.0 2352
4 735.0 801
5 269.0 208
6 40.0 24

Total # of Engagements % Sales per Participants
# of Engagement Types
0 0.0 0.03
1 35914.0 0.06
2 18482.0 0.61
3 8581.0 3.49
4 4357.0 8.86
5 1548.0 23.56
6 211.0 20.83

这是完整的错误:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-211-f0185fe64c1a> in <module>()
12 sns.set_style("whitegrid")
13 # Ploting the histagram for the % of total prospects
---> 14 ax1 = sns.barplot(x=sales_type.index,y='% Sales per Participants', data=sales_type ,ax=ax1)
15 ax1.set(ylabel = '%')
16 ax1.set_title('% Sales per Participants By # of Engagement Types')

ValueError: Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional

我已尝试在 Internet 和 Stack Overflow 上搜索此错误,但没有得到任何结果。有人知道发生了什么吗?

最佳答案

简化问题

我也遇到了这个问题,发现是列名重复造成的。

要重新创建这个:

df = pd.DataFrame({"foo": [1,2,3], "bar": [1,2,3]})
df.rename(columns={'foo': 'bar'}, inplace=True)

bar bar
0 1 1
1 2 2
2 3 3

df.groupby('bar')

ValueError: Grouper for 'bar' not 1-dimensional

就像很多神秘的 pandas 错误一样,这个错误也源于有两个同名的列。

确定您要使用哪一列,重命名或删除另一列并重做操作。

解决方案

像这样重命名列

df.columns = ['foo', 'bar']

foo bar
0 1 1
1 2 2
2 3 3

df.groupby('bar')
<pandas.core.groupby.DataFrameGroupBy object at 0x1066dd950>

关于python - 值错误 : Grouper for <something> not 1-dimensional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298192/

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