简体   繁体   中英

How to drop a row by index from my pandas data-frame to prevent them appearing my my bar chart

I am using df.drop however when I run my code I'm still seeing the "total" on row index 10 in my plot. I want this removed.

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()

You don't need brackets. Also, you need to specify inplace

df.drop(10, inplace=True)

df.drop([10]) creates a copy of df with the row dropped. Try assigning it to a new DataFrame:

df2 = df.drop([10])

then extract the columns from df2 . Or use the inplace argument to permanently modify df :

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

Make a small change in your code. Write df.drop(10,inplace = True) while dropping.

    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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM