gpt4 book ai didi

python - 重新排列周末后移动列以具有相同的数字并遵循数字序列python

转载 作者:行者123 更新时间:2023-12-01 06:45:03 25 4
gpt4 key购买 nike

日期采用日期时间格式周末需要有相同的数字,并将其余日期按数字顺序向下移动。df1

   Date         Number
0. 12-10-2018 1
1. 13-10-2018 2
2. 14-10-2018 3
3. 15-10-2018 4
4. 16-10-2018 5
5. 17-10-2018 6
6. 18-10-2018 7
7. 18-10-2018 8
8. 19-10-2018 9

我的代码:

   i = 1
df1['Number'][0] == 1
for i range(len(df1[0])):
if df1[0[i].weekday()== 4:

df1['Number'].iloc[i+1] = df1['Number'].iloc[i]
elif df1[0][i].weekday() == 5:
df1['Number'].iloc[i+1] = df1['Number'].iloc[i]

这给了我输出我的号码所在的位置:

df1

   Date         Number
0. 12-10-2018 1
1. 13-10-2018 1
2. 14-10-2018 1
3. 15-10-2018 4
4. 16-10-2018 5
5. 17-10-2018 6
6. 18-10-2018 7
7. 19-10-2018 8
8. 20-10-2018 8

我需要输出为:

df1

   Date         Number
0. 12-10-2018 1
1. 13-10-2018 1
2. 14-10-2018 1
3. 15-10-2018 2
4. 16-10-2018 3
5. 17-10-2018 4
6. 18-10-2018 5
7. 19-10-2018 6
8. 20-10-2018 6

非常感谢

最佳答案

我们可以通过分解来修复您的输出

df.Number=df.Number.factorize()[0]+1
df
Date Number
0.0 12-10-2018 1
1.0 13-10-2018 1
2.0 14-10-2018 1
3.0 15-10-2018 2
4.0 16-10-2018 3
5.0 17-10-2018 4
6.0 18-10-2018 5
7.0 19-10-2018 6
8.0 20-10-2018 6

关于python - 重新排列周末后移动列以具有相同的数字并遵循数字序列python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59267630/

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