How to swap month and day values in a DataFrame?

Question:

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  
Asked By: Ridsen

||

Answers:

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)
Answered By: Rishi Singh