简体   繁体   中英

Trying to create a % column on a Pandas Dataframe, but only getting NaN Values

I´m trying to add a percentage column into a dataframe, but when i try to add it to the new column all i get is NaN values

To create the column 'percent_clicked' on the 'clicks_pivot' df:

clicks_pivot['percent_clicked'] = (clicks_pivot.user_id / clicks_pivot.user_id.sum()) * 100

Printing the modified 'clicks_pivot' i get:

utm_source

email 255 NaN

facebook504 NaN

google 680 NaN

twitter 215 NaN

How can i get the % instead of the NaN values?

it works, i tested. Before using the code make sure the column in question is not holding strings instead of ints.

df['b'] = (df['a'] / df['a'].sum()) * 100

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