简体   繁体   English

如何在没有 for 循环的情况下对每一列应用 function 从现有的创建一个新的 pandas DataFrame?

[英]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.

相关问题 将函数应用于pandas数据框中的所有其他列 - Applying function to every other column in pandas dataframe 将函数应用于Pandas中数据框列的每一行 - Applying a function to every row in a dataframe column in Pandas 如何通过使用 function 对另一个现有列中的值进行操作来填充新的 Pandas dataframe? - How can I populate a new Pandas dataframe by using a function to operate on the values in another existing column? 如何通过应用更改公式从另一个现有的创建新的 dataframe? - How do I create a new dataframe from another existing one by applying a changing formula? 是否将函数应用于pandas数据框的每一列而没有for循环? - Apply function to every column of pandas dataframe without for loop? 如何从 pandas dataframe 中的现有列创建新列 - How to create a new column from an existing column in a pandas dataframe 如何使用for循环在pandas数据框中的现有列上创建条件列 - How to create new column conditional on existing columns in pandas dataframe using for loop Pandas:如何在 pandas Z6A8064B5DF4794505500553C47D5 的列上使用 map 来创建新的列? 使用 lambda function 执行此操作时遇到问题 - Pandas: how can I use map on a column in a pandas dataframe to create a new column? Having trouble using a lambda function to do this 如何在 pandas dataframe 中创建新列,该计算发生在除 go 之外的每一行之外 - How to create new column in pandas dataframe of a calculation that happens to every other row except the one where the calculation will go into 如何将数据从 Pandas 数据帧的一列拆分为新数据帧的多列 - How do I split data out from one column of a pandas dataframe into multiple columns of a new dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM