python - Pandas 中的 Rowwise

标签 python pandas

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

数据输入:

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/

相关文章:

python - subprocess.Popen 简单代码不允许我执行 cd(更改目录)

python - 您将如何格式化像这样的 Python 字典这样的 Auth/OAuth header ?

python - pandas to_json 将日期格式更改为 "epoch time"

python - Django Query 设置用于计算每个月的记录

python - 带有元组字符串表示的格式错误的字符串 ValueError ast.literal_eval()

python - 在 Python 中解析轻量级语言

python - 如何在不循环的情况下在 csv 中保存二维关联数组?

python - 在一帧中的 block_wise 索引下连接两个不同长度的数据帧

python - Pandas:在列的应用函数中使用索引值

python - 检查 NaT 或 pandas 时间戳