首页 > 解决方案 > 如何从我的熊猫数据框中按索引删除一行以防止它们出现在我的条形图中

问题描述

我正在使用 df.drop 但是当我运行我的代码时,我仍然在我的情节中看到行索引 10 上的“总数”。我想把它去掉。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv ("https://raw.githubusercontent.com/ryan-j-hope/Shielding-models/master/2020%20Uk%20Population%20By%20Age%20Demographics.csv", encoding='UTF')

df.drop([10])

print(df)

ag = df["Age Group"]

pop = df["Population (%)"]

plt.bar(ag, pop)

plt.show()

标签: pythonpandasmatplotlib

解决方案


你不需要括号。此外,您需要就地指定

df.drop(10, inplace=True)

推荐阅读