我有一个Pandas系列数字,可以说[1,2,3,4,5]
。我想要一种有效的方法来创建一个数据框,其中系列元素的每个组合都通过一个函数传递,结果是对应的数据框元素
可以说函数是
def f1(a,b):
return a*b + 5
然后我希望数据帧如下所示,每个单元格是结合了系列元素的函数调用的结果。
col_1___2___3___4___5__
row|
1|6, 7, 8, 9, 10
2|7, 9, 11, 13, 15
3|8, 11, 14, 17, 20
4|9, 13, 17, 21, 25
5|10, 15, 20, 25, 30
该系列有时可以多达500个元素。
谢谢!
答案 0 :(得分:2)
您可以这样做:
const date = new Date(2020,2,2)
const iso = date.toISOString()
输出:
def f1(a,b):
return a*b + 5
s=[1,2,3,4,5]
df=pd.DataFrame(columns=s, index=s)
df=df.apply(lambda x: f1(x.name, x.index))
答案 1 :(得分:0)
使用熊猫
import pandas as pd
#create pandas dataframe with one column "col_" with data.
df = pd.DataFrame({'col_':list(range(1, 6))})
print(df['col_']) #this is data you provided above.
#Create a new column based on function.
b=1 #constant
df['1_'] = df['col_'].apply(lambda x: x*b+5)
#and another column
df['2_'] = df['col_'].apply(lambda x: x*(b+b)+5)