how to convert a str column to datemonth format
时间: 2023-12-31 10:05:54 浏览: 119
To convert a string column to the date-month format, you can use the `to_datetime` function from the pandas library. Here's an example:
```python
import pandas as pd
# Create an example DataFrame with a string column
df = pd.DataFrame({'Date': ['2022-01-01', '2022-02-01', '2022-03-01']})
# Convert the string column to date-month format
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%b-%Y')
print(df)
```
Output:
```
Date
0 Jan-2022
1 Feb-2022
2 Mar-2022
```
In the above code, we use the `to_datetime` function to convert the string column `Date` to a datetime format. Then, we use the `dt.strftime('%b-%Y')` method to format the datetime values as month abbreviations followed by the year. The result is assigned back to the `Date` column in the DataFrame.
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)