英文:
How to swap month and day values in a DataFrame?
问题
我有一个DataFrame,其中第一列包含日期和时间信息。
但是对我来说,这些信息被错误地识别了。
当我从数据库中提取信息并将数据发送到DataFrame表时,它看起来像是我“交换了位置” - “天”和“月”。
“天”和“月”被颠倒了 - 天的值必须与月的值相同。
“月”和“天”被颠倒了 - 月的值必须是天的值。
如何交换并使天像月一样,月像天一样?
在DataFrame表中,一列被识别为类型 - datetime。
但是月和日的识别是错误的 - 它们被交换了。
如何在DataFrame中交换月和日的值?
--
之前
id datetime temp_narvoz q_virob
0 1 2023-01-02 10:00:00 1.4 0.688331
1 2 2023-01-02 11:00:00 1.4 0.800867
2 3 2023-01-02 12:00:00 1.4 0.810746
3 4 2023-01-02 13:00:00 1.4 0.805522
4 5 2023-01-02 14:00:00 2.1 0.802979
1 Jan. 2, 2023, 10 a.m. 1.4 0.6883313373044648
2 Jan. 2, 2023, 11 a.m. 1.4 0.8008674575779552
3 Jan. 2, 2023, noon 1.4 0.8107462069822856
4 Jan. 2, 2023, 1 p.m. 1.4 0.8055222730239303
5 Jan. 2, 2023, 2 p.m. 2.1 0.8029786055112401
6 Jan. 2, 2023, 3 p.m. 2.1 0.7854097839013776
7 Jan. 2, 2023, 4 p.m. 2.1 0.7950360149694395
8 Jan. 2, 2023, 5 p.m. 1.6 0.8296386761628508
9 Jan. 2, 2023, 6 p.m. 1.6 0.83005095964985
10 Jan. 2, 2023, 7 p.m. 1.6 0.8569995535066821
11 Jan. 2, 2023, 8 p.m. -0.5 0.8710515387962285
12 Jan. 2, 2023, 9 p.m. -0.5 0.864164249456128
13 Jan. 2, 2023, 10 p.m. -0.5 0.8514567681549778
14 Jan. 2, 2023, 11 p.m. 0.3 0.8078847547912567
15 Feb. 2, 2023, midnight 0.3 0.7834063591629548
--
之后
id datetime temp_narvoz q_virob
0 1 2023-02-01 10:00:00 1.4 0.688331
1 2 2023-02-01 11:00:00 1.4 0.800867
2 3 2023-02-01 12:00:00 1.4 0.810746
3 4 2023-02-01 13:00:00 1.4 0.805522
4 5 2023-02-01 14:00:00 2.1 0.802979
1 Feb. 1, 2023, 10 a.m. 1.4 0.6883313373044648
2 Feb. 1, 2023, 11 a.m. 1.4 0.8008674575779552
3 Feb. 1, 2023, noon 1.4 0.8107462069822856
4 Feb. 1, 2023, 1 p.m. 1.4 0.8055222730239303
5 Feb. 1, 2023, 2 p.m. 2.1 0.8029786055112401
6 Feb. 1, 2023, 3 p.m. 2.1 0.7854097839013776
7 Feb. 1, 2023, 4 p.m. 2.1 0.7950360149694395
8 Feb. 1, 2023, 5 p.m. 1.6 0.8296386761628508
9 Feb. 1, 2023, 6 p.m. 1.6 0.83005095964985
10 Feb. 1, 2023, 7 p.m. 1.6 0.8569995535066821
11 Feb. 1, 2023, 8 p.m. -0.5 0.8710515387962285
12 Feb. 1, 2023, 9 p.m. -0.5 0.864164249456128
13 Feb. 1, 2023, 10 p.m. -0.5 0.8514567681549778
14 Feb. 1, 2023, 11 p.m. 0.3 0.8078847547912567
15 Feb. 1, 2023, midnight 0.3 0.7834063591629548
英文:
I have a DataFrame in which the first column contains information in the form of a date and time.
But for me this information is not recognized correctly.
When I took information from the database and sent the data to the DataFrame table, it looks like I "swapped places" - "day" and "month".
"Day" and "month" are reversed - the day value must be the same value as the month.
"Month" and "day" are reversed - the month value must be a day value.
How can I flip and make the day be like a month, and the month like a day?
In a DataFrame table, a column is recognized as a type - datetime.
But the recognition of the month and day is wrong - they are swapped.
How to swap month and day values in a DataFrame?
now the date time is being recognized so
df['datetime'] = pd.to_datetime(df["datetime"].dt.strftime('%Y-%m-%d'))
and the real location of the month and day is
df['datetime'] = pd.to_datetime(df["datetime"].dt.strftime('%Y-%d-%m'))
--
before
id datetime temp_narvoz q_virob
0 1 2023-01-02 10:00:00 1.4 0.688331
1 2 2023-01-02 11:00:00 1.4 0.800867
2 3 2023-01-02 12:00:00 1.4 0.810746
3 4 2023-01-02 13:00:00 1.4 0.805522
4 5 2023-01-02 14:00:00 2.1 0.802979
1 Jan. 2, 2023, 10 a.m. 1.4 0.6883313373044648
2 Jan. 2, 2023, 11 a.m. 1.4 0.8008674575779552
3 Jan. 2, 2023, noon 1.4 0.8107462069822856
4 Jan. 2, 2023, 1 p.m. 1.4 0.8055222730239303
5 Jan. 2, 2023, 2 p.m. 2.1 0.8029786055112401
6 Jan. 2, 2023, 3 p.m. 2.1 0.7854097839013776
7 Jan. 2, 2023, 4 p.m. 2.1 0.7950360149694395
8 Jan. 2, 2023, 5 p.m. 1.6 0.8296386761628508
9 Jan. 2, 2023, 6 p.m. 1.6 0.83005095964985
10 Jan. 2, 2023, 7 p.m. 1.6 0.8569995535066821
11 Jan. 2, 2023, 8 p.m. -0.5 0.8710515387962285
12 Jan. 2, 2023, 9 p.m. -0.5 0.864164249456128
13 Jan. 2, 2023, 10 p.m. -0.5 0.8514567681549778
14 Jan. 2, 2023, 11 p.m. 0.3 0.8078847547912567
15 Feb. 2, 2023, midnight 0.3 0.7834063591629548
--
after
id datetime temp_narvoz q_virob
0 1 2023-02-01 10:00:00 1.4 0.688331
1 2 2023-02-01 11:00:00 1.4 0.800867
2 3 2023-02-01 12:00:00 1.4 0.810746
3 4 2023-02-01 13:00:00 1.4 0.805522
4 5 2023-02-01 14:00:00 2.1 0.802979
1 Feb. 1, 2023, 10 a.m. 1.4 0.6883313373044648
2 Feb. 1, 2023, 11 a.m. 1.4 0.8008674575779552
3 Feb. 1, 2023, noon 1.4 0.8107462069822856
4 Feb. 1, 2023, 1 p.m. 1.4 0.8055222730239303
5 Feb. 1, 2023, 2 p.m. 2.1 0.8029786055112401
6 Feb. 1, 2023, 3 p.m. 2.1 0.7854097839013776
7 Feb. 1, 2023, 4 p.m. 2.1 0.7950360149694395
8 Feb. 1, 2023, 5 p.m. 1.6 0.8296386761628508
9 Feb. 1, 2023, 6 p.m. 1.6 0.83005095964985
10 Feb. 1, 2023, 7 p.m. 1.6 0.8569995535066821
11 Feb. 1, 2023, 8 p.m. -0.5 0.8710515387962285
12 Feb. 1, 2023, 9 p.m. -0.5 0.864164249456128
13 Feb. 1, 2023, 10 p.m. -0.5 0.8514567681549778
14 Feb. 1, 2023, 11 p.m. 0.3 0.8078847547912567
15 Feb. 1, 2023, midnight 0.3 0.7834063591629548
答案1
得分: 0
import pandas as pd
df = pd.DataFrame({'date': ['11/02/2022', '12/02/2023', '13/02/2023']})
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(lambda x: x.replace(day=x.month,
month=x.day))
print(df)
英文:
import pandas as pd
df = pd.DataFrame({'date': ['11/02/2022', '12/02/2023', '13/02/2023']})
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(lambda x: x.replace(day=x.month,
month=x.day))
print(df)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论