简体   繁体   中英

Drop decimals and add commas Pandas

I want a column in my Dataframe to have no decimals but have commas. It's for a bar chart. Every time I add the commas I get the decimals. Even if I convert the column to integers first. Here is the DataFrame and what I tried that is not working!

df = pd.read_csv('https://github.com/ngpsu22/indigenous-peoples-day/raw/main/native_medians_means')

summary.med_resources_per_person.astype(int)
summary["med_resources_per_person"] = (summary["med_resources_per_person"].apply(lambda x : " 
                                       {:,}".format(x)))

You're not actually changing the dtype to int inside of the dataframe. You'll need to assign it back to the column:

df = pd.read_csv('https://github.com/ngpsu22/indigenous-peoples-day/raw/main/native_medians_means')

df["med_resources_per_person"] = df["med_resources_per_person"].astype(int)
df["med_resources_per_person"].apply(lambda x : "{:,}".format(x))

Or a little bit more concise:

df = pd.read_csv('https://github.com/ngpsu22/indigenous-peoples-day/raw/main/native_medians_means')

df["med_resources_per_person"] = df["med_resources_per_person"].astype(int).apply("{:,}".format)

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