gpt4 book ai didi

python - Pandas 中的 Rowwise

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:58 25 4
gpt4 key购买 nike

这是我之前问过的问题,但我用错误的方式解释了它,所以我将再次打开一个新问题。感谢您的帮助和时间!

数据输入:

df=pd.DataFrame({'variable':["A","A","B","B","C","D","E","E","E","F","F","G"],'weight':[2,2,0,0,1,3,3,1,5,0,0,4]})
df
Out[447]:
variable weight
0 A 2
1 A 2
2 B 0
3 B 0
4 C 1
5 D 3
6 E 3
7 E 1# If value more than 2 , out put should be 0
8 E 5
9 F 0
10 F 0
11 G 4

预期输出:

df
Out[449]:
variable weight NEW
0 A 2 1
1 A 2 1
2 B 0 1
3 B 0 1
4 C 1 1
5 D 3 ERROR
6 E 3 ERROR
7 E 1 1
8 E 5 1
9 F 0 1
10 F 0 1
11 G 4 ERROR

我现在的方法(丑陋..):

l1=[]
for i in df.variable.unique():
temp=df.loc[df.variable==i]
l2 = []
for j in range(len(temp)):
print(i,j)

if temp.iloc[j,1]<=2 :
l2.append(1)
elif temp.iloc[j,1]>2 and j==0:
l2.append('ERROR')
elif temp.iloc[j,1]>2 and j > 0 :
if l2[j - 1] == 1:
l2.append(1)
else:
l2.append(0)
print(l2)
l1.extend(l2)
df['NEW']=l1

我的问题:

第一。如果我想使用 groupby ,我怎样才能让 per-calculated result 参与到 future 的计算中,以便在这里得到 NEW 列。

第二。有没有像.Last.value这样的pandas函数在 R 中?


我会在这里解释条件:

1.如果weight的值小于2总是应该为1

2.如果权重的第一个值大于2则返回ERROR

3.如果前一个得到'ERROR'并且当前行的权重值大于2,则返回0

请将输入更改为:

df=pd.DataFrame({'variable':["A","A","B","B","C","D","E","E","E","F","F","G"],'weight':[2,2,0,0,1,3,3,9,5,0,0,4]})

最佳答案

n = 2  # `Error` weight filter.
# Get boolean index of whether weight of first item in group is greater than `n`.
mask = df.loc[[idx[0] for idx in df.groupby('variable')['weight'].groups.values()], 'weight'].gt(n)
df = df.assign(New=1)
df.loc[mask[mask].index, 'New'] = 'ERROR'
>>> df
variable weight New
0 A 2 1
1 A 2 1
2 B 0 1
3 B 0 1
4 C 1 1
5 D 3 ERROR
6 E 3 ERROR
7 E 1 1
8 E 5 1
9 F 0 1
10 F 0 1
11 G 4 ERROR

关于python - Pandas 中的 Rowwise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46227296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com