英文:
Pandas apply function read in list horizontally as an input
问题
以下是您要翻译的内容:
"Is there a way for the apply function to read in a list of values horizontally row-wise from the 3 columns ['A', 'B', 'C'] into a list 'x' as well as number from 'Val' as 'y' to the apply function to create a new column 'Result'?
I've presented the column f as more simplistic, I just need to know how to read a list/series into the function f
import pandas as pd
cols=['Name','A','B','C','Type','Val']
data = [['Front',1,2,3,'Up',11],
['Front',4,5,6,'Dw',22]]
df = pd.DataFrame(data, columns=cols)
def f(x,y):
return sum(x)*y
not sure this is correct
df['Result'] = df.apply(lambda row: f(row[['A','B','C']],row['Val'],axis=1))
Initial Data:
Name A B C Type Val
0 Front 1 2 3 Up 11
1 Front 4 5 6 Dw 22
Desired Result:
Name A B C Type Val Result
0 Front 1 2 3 Up 11 66
1 Front 4 5 6 Dw 22 330"
英文:
Is there a way for the apply function to read in a list of values horizontally row-wise from the 3 columns ['A','B','C'] into a list 'x' as well as number from 'Val' as 'y' to the apply function to create a new column 'Result'?
I've presented the column f as more simplistic, I just need to know how to read a list/series into the function f
import pandas as pd
cols=['Name','A','B','C','Type','Val']
data = [['Front',1,2,3,'Up',11],
['Front',4,5,6,'Dw',22]]
df = pd.DataFrame(data, columns=cols)
def f(x,y):
return sum(x)*y
not sure this is correct
df['Result'] = df.apply(lambda row: f(row[['A','B','C']],row['Val'],axis=1))
Initial Data:
Name A B C Type Val
0 Front 1 2 3 Up 11
1 Front 4 5 6 Dw 22
Desired Result:
Name A B C Type Val Result
0 Front 1 2 3 Up 11 66
1 Front 4 5 6 Dw 22 330
答案1
得分: 3
你可以直接对此进行向量化!
df["Result"] = (df["A"] + df["B"] + df["C"]) * df["Val"]
英文:
You can directly vectorize this!
df["Result"] = (df["A"] + df["B"] + df["C"]) * df["Val"]
答案2
得分: 3
import pandas as pd
cols = ['Name', 'A', 'B', 'C', 'Type', 'Val']
data = [['Front', 1, 2, 3, 'Up', 11],
['Front', 4, 5, 6, 'Dw', 22]]
df = pd.DataFrame(data, columns=cols)
def f(x, y):
return sum(x) * y
df['Result'] = df.apply(lambda row: f(row[['A', 'B', 'C']].values.tolist(), row['Val']), axis=1)
print(df)
结果:
Name A B C Type Val Result
0 Front 1 2 3 Up 11 66
1 Front 4 5 6 Dw 22 330
英文:
import pandas as pd
cols = ['Name', 'A', 'B', 'C', 'Type', 'Val']
data = [['Front', 1, 2, 3, 'Up', 11],
['Front', 4, 5, 6, 'Dw', 22]]
df = pd.DataFrame(data, columns=cols)
def f(x, y):
return sum(x) * y
df['Result'] = df.apply(lambda row: f(row[['A', 'B', 'C']].values.tolist(), row['Val']), axis=1)
print(df)
Result:
Name A B C Type Val Result
0 Front 1 2 3 Up 11 66
1 Front 4 5 6 Dw 22 330
答案3
得分: 0
请尝试这样做:
df['Result'] = df.apply(lambda row: f(row[['A', 'B', 'C']], row['Val']), axis=1)
import pandas as pd
cols = ['Name', 'A', 'B', 'C', 'Type', 'Val']
data = [['Front', 1, 2, 3, 'Up', 11],
['Front', 4, 5, 6, 'Dw', 22]]
df = pd.DataFrame(data, columns=cols)
def f(x, y):
return sum(x) * y
df['Result'] = df.apply(lambda row: f(row[['A', 'B', 'C']], row['Val']), axis=1)
print(df)
英文:
Try this:
df['Result'] = df.apply(lambda row: f(row[['A', 'B', 'C']], row['Val']), axis=1)
import pandas as pd
cols = ['Name', 'A', 'B', 'C', 'Type', 'Val']
data = [['Front', 1, 2, 3, 'Up', 11],
['Front', 4, 5, 6, 'Dw', 22]]
df = pd.DataFrame(data, columns=cols)
def f(x, y):
return sum(x) * y
df['Result'] = df.apply(lambda row: f(row[['A', 'B', 'C']], row['Val']), axis=1)
print(df)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论