[英]how to use a map function to convert all negative numbers in a Data Frame to the mean of all the numbers in python pandas?
我这样做了,但我将如何获得所有元素的平均值?
import pandas as pd
import numpy as np
df1=pd.DataFrame([[-1,2,-3,4,-4],-3,-6,-4,7,8],[-2,3,5,8,2]])
df1=df1[df1 < 0]=df1.applymap(np.mean)
print(df1)
如果转换所有数据帧到numpy的值,然后用mean
默认axis=None
均值的所有值:
df1[df1 < 0] = df1.to_numpy().mean()
#oldier pandas versions
#df1[df1 < 0] = df1.values.mean()
print (df1)
0 1 2 3 4
0 1.066667 2.000000 1.066667 4 1.066667
1 1.066667 1.066667 1.066667 7 8.000000
2 1.066667 3.000000 5.000000 8 2.000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.