我想使用一个生成多个输出的函数在现有数据帧中创建多个新列
例如,假设我有一个测试函数,它输出两个东西:
def testfunc (TranspoId, LogId):
thing1 = TranspoId + LogId
thing2 = LogId - TranspoId
return thing1, thing2
我可以将这些返回的输出赋给两个不同的变量,如下所示:
Thing1,Thing2 = testfunc(4,28)
print(Thing1)
print(Thing2)
我尝试通过以下方式使用数据帧执行此操作:
data = {'Name':['Picard','Data','Guinan'],'TranspoId':[1,2,3],'LogId':[12,14,23]}
df = pd.DataFrame(data, columns = ['Name','TranspoId','LogId'])
print(df)
df['thing1','thing2'] = df.apply(lambda row: testfunc(row.TranspoId, row.LogId), axis=1)
print(df)
我想要的是这样的东西:
data = {'Name':['Picard','Data','Guinan'],'TranspoId':[1,2,3],'LogId':[12,14,23], 'Thing1':[13,16,26], 'Thing2':[11,12,20]}
df = pd.DataFrame(data, columns=['Name','TranspoId','LogId','Thing1','Thing2'])
print(df)
在现实世界中,这个函数需要做很多繁重的工作,我不能让它运行两次,每次为df添加一个新变量都运行一次
几个小时来我一直在用这个打自己的头。如有任何见解,将不胜感激
函数应该返回一个序列,该序列在一次过程中计算新列。然后可以使用pandas.apply()添加新字段
输出:
我相信最好的方法是改变顺序,制作一个能与级数一起工作的函数
相关问题 更多 >
编程相关推荐