如何从我的熊猫数据框中按索引删除一行,以防止它们出现在我的条形图中



我正在使用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()

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

df.drop(10, inplace=True)

df.drop([10])创建一个删除行的df副本。尝试将其分配给新的DataFrame:

df2 = df.drop([10])

然后从CCD_ 3中提取列。或者使用inplace参数永久修改df:

df.drop([10], inplace=True)

对代码进行一个小的更改。丢弃时写入df.drop(10,inplace=True(。

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,inplace = True)
print(df)
ag = df["Age Group"]
pop = df["Population (%)"]
plt.bar(ag, pop)
plt.show()

最新更新