简体   繁体   English

试图在 Pandas 数据帧上创建一个 % 列,但只获得 NaN 值

[英]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我正在尝试将百分比列添加到数据框中,但是当我尝试将其添加到新列时,我得到的只是 NaN 值

To create the column 'percent_clicked' on the 'clicks_pivot' df:要在 'clicks_pivot' df 上创建列 'percent_clicked':

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

Printing the modified 'clicks_pivot' i get:打印修改后的“clicks_pivot”我得到:

utm_source utm_source

email 255 NaN电子邮件 255 NaN

facebook504 NaN facebook504 南

google 680 NaN谷歌 680 NaN

twitter 215 NaN推特 215 NaN

How can i get the % instead of the NaN values?我怎样才能得到 % 而不是 NaN 值?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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