英文:
How do I change a pandas row value, for a certain column, for a certrain date (datetimeindex) in a dataframe?
问题
以下是您要翻译的内容:
我有一个类似这样的 pandas 数据框:
日期 交付
2020-01-01 1
2020-01-01 11
2020-01-01 10
2020-01-01 9
2020-01-01 8
..
2023-03-02 5
2023-03-02 4
2023-03-02 3
2023-03-02 2
2023-03-02 11
索引是 DateTimeIndex 但不是唯一的。我有一个日期列表(date_adj)来自数据框,我想为这些日期更改 '交付' 列。我尝试了以下代码:
for i in date_adj:
for x in range(1,11):
df.loc[((df.index == i) & (df.交付 == x)), ['交付']] = (df.交付 + 1)
我收到以下错误消息:KeyError: "None of [Index([(1, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 6, 9, 8, 7, 5, 4, 3, 2, 10, 11, 4, 1, 5, 6, 7, 8, 1, 9, 10, 11, 3, 2, 2, 7, 3, 4, 5, 6, 8, 9, 10, 11, 1, 1, 3, 4, 5, 6, 7, 8, 9, 10, 2, 11, 2, 4, 5, 6, 7, 8, 9, 10, 11, 3, 11, 10, 9, 8, 7, 2, 5, 4, 3, 1, 2, 6, 1, 3, 7, 4, 5, 6, 8, 10, 11, 9, 4, 9, 8, 7, 6, 5, 4, 3, 1, 5, 11, 9, ...)], dtype='object')] are in the [columns]"
为了说明,我希望结果如下,给定列表中的日期为 '2023-03-02':
日期 交付
2020-01-01 1
2020-01-01 11
2020-01-01 10
2020-01-01 9
2020-01-01 8
..
2023-03-02 6
2023-03-02 5
2023-03-02 4
2023-03-02 3
2023-03-02 12
帮助将不胜感激。
英文:
I have a pd like this:
DATE delivery
2020-01-01 1
2020-01-01 11
2020-01-01 10
2020-01-01 9
2020-01-01 8
..
2023-03-02 5
2023-03-02 4
2023-03-02 3
2023-03-02 2
2023-03-02 11
Index is DateTimeIndex but not unique. I have a list (date_adj) of a few dates from the df, and I want to change the 'delivery' column for those dates. I tried:
for i in date_adj:
for x in range(1,11):
df.loc[((df.index == i) & (df.delivery == x)),
[df.delivery]] = (df.delivery + 1)
I get the following error message: KeyError: "None of [Index([(1, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 6, 9, 8, 7, 5, 4, 3, 2, 10, 11, 4, 1, 5, 6, 7, 8, 1, 9, 10, 11, 3, 2, 2, 7, 3, 4, 5, 6, 8, 9, 10, 11, 1, 1, 3, 4, 5, 6, 7, 8, 9, 10, 2, 11, 2, 4, 5, 6, 7, 8, 9, 10, 11, 3, 11, 10, 9, 8, 7, 2, 5, 4, 3, 1, 2, 6, 1, 3, 7, 4, 5, 6, 8, 10, 11, 9, 4, 9, 8, 7, 6, 5, 4, 3, 1, 5, 11, 9, ...)], dtype='object')] are in the [columns]"
For illustrational purposes, I would like the result to look like this, given the date in the list is '2023-03-02':
DATE delivery
2020-01-01 1
2020-01-01 11
2020-01-01 10
2020-01-01 9
2020-01-01 8
..
2023-03-02 6
2023-03-02 5
2023-03-02 4
2023-03-02 3
2023-03-02 12
Help would be very appreciated.
答案1
得分: 1
这不是完全清楚,但我认为你想要的是:
```python
date_adj = ['2020-01-01', '2020-01-02']
df.loc[df['DATE'].isin(date_adj) & df['delivery'].between(1, 11), 'delivery'] += 1
输出:
DATE delivery
0 2020-01-01 2
1 2020-01-01 12
2 2020-01-01 11
3 2020-01-01 10
4 2020-01-01 9
5 2023-03-02 5
6 2023-03-02 4
7 2023-03-02 3
8 2023-03-02 2
9 2023-03-02 11
<details>
<summary>英文:</summary>
It's not fully clear, but I think that you want:
date_adj = ['2020-01-01', '2020-01-02']
df.loc[df['DATE'].isin(date_adj) & df['delivery'].between(1, 11), 'delivery'] += 1
Output:
DATE delivery
0 2020-01-01 2
1 2020-01-01 12
2 2020-01-01 11
3 2020-01-01 10
4 2020-01-01 9
5 2023-03-02 5
6 2023-03-02 4
7 2023-03-02 3
8 2023-03-02 2
9 2023-03-02 11
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论