[英]Pandas Dataframe - multiply column by a value
I am trying to identify columns that contain the letter 'k', remove 'k' from the star rating column and multiply the result by 5.我正在尝试识别包含字母“k”的列,从星级评级列中删除“k”并将结果乘以 5。
When I multiply, instead of multiplying the column value, the value is repeated 5 times.当我乘法时,不是乘以列值,而是将值重复 5 次。
I've tried multiplying in other ways (use numpy, use * to multiply) but am getting the same result.我试过以其他方式相乘(使用 numpy,使用 * 相乘)但得到的结果相同。
Any suggestions?有什么建议么? Image of code: [ 1 ]
代码图像:[ 1 ]
df_starrating ['StarRating'] = ['1.1k stars','900 stars','2.5k stars']
df_starrating['StarRating'] = df_starrating['StarRating'].str.replace('stars','')
if df_starrating['StarRating'].str.contains('k').any():
df_starrating['StarRating'] = df_starrating['StarRating'].str.replace('k','')
df_starrating['StarRating'] = df_starrating['StarRating'].multiply(5,axis = 'index')
Output: Output:
1.1k, 1.1k, 1.1k, 1.1k, 1.1k 1.1k, 1.1k, 1.1k, 1.1k, 1.1k
900,900,900,900,900 900,900,900,900,900
2.5,2.5,2.5,2.5,2.5 2.5,2.5,2.5,2.5,2.5
You can use the .apply(function)
method.您可以使用
.apply(function)
方法。 Something like this should work:这样的事情应该有效:
def reformat(s):
if s.find('k')!=-1
s=s.replace('k', '')
return float(s)*5
df_starrating['StarRating'] = df_starrating['StarRating'].apply(reformat)
I believe you are multiplying a string.我相信你正在乘以一个字符串。 Try converting to a float first eg
首先尝试转换为浮点数,例如
df_starrating['StarRating'] = df_starrating['StarRating'].astype(float).multiply(5,axis = 'index')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.