Pandas 的 apply 函数将列表水平读入作为输入。

huangapple go评论79阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年7月11日 08:03:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定