[英]How can I create a new pandas DataFrame out of an existing one applying a function to every column without a for loop?
A simplified script I have now working is as follows:我现在使用的简化脚本如下:
columns = df.columns.tolist()
df1=pd.DataFrame()
for i in columns:
df1[i]=[random.uniform(-1*(df[i].std()*3),(df[i].std()*3))+df[i].mean()]
How can I get the same result (a one row dataframe) with a simpler, more efficient code?如何使用更简单、更高效的代码获得相同的结果(单行数据框)?
Try with apply
:尝试apply
:
df1 = df.apply(lambda x: random.uniform(-3*x.std(),3*x.std())+x.mean())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.