I'm trying to read a CSV file to a pandas dataframe. The file has dates as 4 digit numbers (e.g., 2001, 2002). Is there a way to convert the 4 digit year to a full year-end format date (12/31/2001) via the parse_date in read_csv?
CodePudding user response:
I had a problem like that and solve like this:
I had a variable called my_year
date = ('12' "/" '31' "/" my_year)
It worked for me
CodePudding user response:
You can use apply and give either a function or a lambda
function
Dummy data:
df = pd.DataFrame([2020 for _ in range(20)], columns=["year"])
df
Output:
year
0 2020
1 2020
2 2020
3 2020
4 2020
5 2020
6 2020
7 2020
8 2020
9 2020
10 2020
11 2020
12 2020
13 2020
14 2020
15 2020
16 2020
17 2020
18 2020
19 2020
apply
Here we apply a string concatenation using fstring:
df["year"] = df["year"].apply(lambda row: f'12/31/{row}')
df
Output:
year
0 12/31/2020
1 12/31/2020
2 12/31/2020
3 12/31/2020
4 12/31/2020
5 12/31/2020
6 12/31/2020
7 12/31/2020
8 12/31/2020
9 12/31/2020
10 12/31/2020
11 12/31/2020
12 12/31/2020
13 12/31/2020
14 12/31/2020
15 12/31/2020
16 12/31/2020
17 12/31/2020
18 12/31/2020
19 12/31/2020