pandas:代码在pycharm中工作,但在replit中不工作

sirbozc5  于 2022-12-21  发布在  PyCharm
关注(0)|答案(1)|浏览(172)

我尝试在replit中运行以下代码:

df_cat = pd.melt(df, id_vars='cardio', value_vars=['active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke'])
    

c = df_cat.groupby(['cardio','variable','value'], as_index=False,level=None, sort=True, group_keys=True, observed=False).size()
cardio_0 = c[c['cardio']==0]
cardio_1 = c[c['cardio']==1]

这段代码在pycharm中运行得很好,但在replit中不起作用,我不知道还能做什么。

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    medical_data_visualizer.draw_cat_plot()
  File "/home/runner/boilerplate-medical-data-visualizer/medical_data_visualizer.py", line 33, in draw_cat_plot
    cardio_0 = c(c['cardio']==0)
  File "/home/runner/boilerplate-medical-data-visualizer/venv/lib/python3.8/site-packages/pandas/core/series.py", line 1071, in __getitem__
    result = self.index.get_value(self, key)
  File "/home/runner/boilerplate-medical-data-visualizer/venv/lib/python3.8/site-packages/pandas/core/indexes/multi.py", line 1510, in get_value
    raise e1
  File "/home/runner/boilerplate-medical-data-visualizer/venv/lib/python3.8/site-packages/pandas/core/indexes/multi.py", line 1494, in get_value
    return self._engine.get_value(s, k)
  File "pandas/_libs/index.pyx", line 80, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 88, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 670, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
KeyError: 'cardio'

同样的代码在pycharm中也能很好地工作

xxslljrj

xxslljrj1#

您可以使用c.reset_index()重置c的索引,这将为MultiIndex的每个级别创建一个列的新DataFrame,并允许您使用'cardio'列为c建立索引,就像您当前正在做的那样。

df_cat = pd.melt(df, id_vars='cardio', value_vars=['active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke'])

c = df_cat.groupby(['cardio','variable','value'], as_index=False,level=None, sort=True, group_keys=True, observed=False).size()

# Reset the index of c to create a DataFrame with columns for each level of the MultiIndex
c = c.reset_index()

cardio_0 = c[c['cardio']==0]
cardio_1 = c[c['cardio']==1]

print(cardio_0)
print(cardio_1)
    • 输出:**
index  cardio     variable  value  size
0       0       active      0     2
1       0       active      1     1
2       0         alco      0     2
3       0         alco      1     1
4       0  cholesterol      0     2
5       0  cholesterol      1     1
6       0         gluc      1     3
7       0   overweight      1     3
8       0        smoke      0     1
9       0        smoke      1     2

index  cardio     variable  value  size
10       1       active      0     1
11       1       active      1     2
12       1         alco      0     2
13       1         alco      1     1
14       1  cholesterol      0     1
15       1  cholesterol      1     2
16       1         gluc      0     3
17       1   overweight      0     3
18       1        smoke      0     2
19       1        smoke      1     1

相关问题