如何在数据框中为特定列、特定日期(DatetimeIndex)更改pandas行值?

huangapple go评论52阅读模式
英文:

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&#39;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>

huangapple
  • 本文由 发表于 2023年3月3日 20:48:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定