pandas “groupby” 在标题中产生了一个无法访问或删除的级别

df_india = pd.read_csv('fakepath\file.csv')df_india.head()

enter image description here

清理后,数据框架看起来像这样

df_india.head(5)

enter image description here

由于我想按州进行分组

df_india = df_india.groupby(by=['State']).sum()df_india.head(5)

enter image description here

这里出现了一个不必要的级别,但我无法访问或删除 ‘State’ 级别。我希望这两个列在数据框的标题中处于同一级别

我尝试重置索引,然后标题看起来像单级标题。

df_india.reset_index().head(2)

enter image description here

但仍然无法访问 ‘State’ 列

df_india['State']

enter image description here


回答:

只需在 groupby() 方法中使用 as_index 参数,并将其设置为 False

df_india = df_india.groupby(by=['State'],as_index=False).sum()

现在编写:

df_india['State']

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注