gpt4 book ai didi

python - 根据同一列的先前值更改Python数据框中的列

转载 作者:行者123 更新时间:2023-12-01 09:23:35 25 4
gpt4 key购买 nike

我在 pandas python 中有一个数据框,如下所示 data frame

<table style="width:100%">
<tr>
<th>ID</th>
<th>AGE</th>
<th>GENDER</th>
<th>TIME</th>
<th>CODE</th>
</tr>
<tr>
<td>1</td>
<td>66</td>
<td>M</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>66</td>
<td>M</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>66</td>
<td>M</td>
<td>3</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>66</td>
<td>M</td>
<td>4</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>20</td>
<td>F</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>2</td>
<td>20</td>
<td>F</td>
<td>2</td>
<td>0</td>
<tr>
<td>2</td>
<td>20</td>
<td>F</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>3</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>4</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>5</td>
<td>1</td>
</tr>
</table>

1 66 M 1 11 66米2 11 66 米 3 12 20 F 1 02 20 F 2 02 20 F 3 02 20 F 4 03 18 F 1 13 18 F 2 13 18 F 3 13 18 F 4 1

我需要根据以下内容更改最后一列(只要“CODE”列为 1,则将该 ID 的最后一行保留为 1,并将前面的行更改为零)

<table style="width:100%">
<tr>
<th>ID</th>
<th>AGE</th>
<th>GENDER</th>
<th>TIME</th>
<th>CODE</th>
</tr>
<tr>
<td>1</td>
<td>66</td>
<td>M</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>66</td>
<td>M</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>66</td>
<td>M</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
<td>66</td>
<td>M</td>
<td>4</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>20</td>
<td>F</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>2</td>
<td>20</td>
<td>F</td>
<td>2</td>
<td>0</td>
<tr>
<td>2</td>
<td>20</td>
<td>F</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>2</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>3</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>4</td>
<td>0</td>
</tr>
<tr>
<td>3</td>
<td>18</td>
<td>F</td>
<td>5</td>
<td>1</td>
</tr>
</table>

如何使用 pandas 来做到这一点?

查找后我发现这行代码删除了每组的最后一行 dfnew = (df.groupby('ID').apply(lambda x: x.iloc[:-1] if len(x)>1 else x))

提前致谢

最佳答案

通过1过滤获取索引,并通过ID删除重复drop_duplicates :

i = df[df['CODE'] == 1].drop_duplicates(subset=['ID'], keep='last').index

先将列设置为0,然后替换为i:

df['CODE'] = 0
df.loc[i, 'CODE'] = 1

另一个解决方案是创建 bool 掩码并将其转换为 ints:

m = (df['CODE'] == 1) & ~df['ID'].duplicated(keep='last')
print (m)
0 False
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 False
9 False
10 True
dtype: bool

df['CODE'] = m.astype(int)
<小时/>
print (df)
ID AGE GENDER TIME CODE
0 1 66 M 1 0
1 1 66 M 2 0
2 1 66 M 3 1
3 2 20 F 1 0
4 2 20 F 2 0
5 2 20 F 3 0
6 2 20 F 4 0
7 3 18 F 1 0
8 3 18 F 2 0
9 3 18 F 3 0
10 3 18 F 4 1

关于python - 根据同一列的先前值更改Python数据框中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50622746/

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