Issue
I have the following dataframe:
date sea_temp_diff wind_distance_diff wind_speed_diff temp_diff humidity_diff current_distance_diff current_speed_diff month year
0 2017.04 2.600000 20.000000 -4.000000 0.000000 0.000000 -90.000000 -1.000000e-01 04 2017
1 2017.05 -1.553333 8.666667 0.833333 -0.766667 0.000000 95.000000 -1.300000e-01 05 2017
2 2017.06 -1.551852 -10.370370 -1.888889 -0.888889 0.000000 179.629630 -2.851852e-01 06 2017
3 2017.07 -1.653571 -20.000000 -2.357143 -1.071429 0.000000 77.142857 -3.000000e-01 07 2017
4 2017.08 -1.287500 -8.333333 -1.916667 -1.166667 0.000000 88.333333 -2.041667e-01 08 2017
5 2017.09 -1.262500 3.333333 -1.375000 0.166667 615.833333 13.333333 -3.750000e-02 09 2017
6 2017.10 -0.809677 -146.451613 -161.548387 0.419355 -7.064516 -79.354839 -1.613645e+02 .1 2017
I generated this dataframe with:
australia_overview_clean['month'] = australia_overview_clean['date'].astype(str).str[-2:]
australia_overview_clean
For some reason though, instead of returning the '10' month, I get '0.1'. while it is mathematically identical to 10 or 0.10, I wouldn't mind finding a way to put 10 in its place instead. Is there a way I can fix this?
Solution
You can get your year and month from the date by splitting on ".
".
ljust
is used to make sure the date column is 7 characters long before splitting.
df[["year", "month"]] = df["date"].astype(str).str.ljust(7,"0").str.split(".", expand=True).astype(int)
>>> df[["date", "year", "month"]]
date year month
0 2017.04 2017 4
1 2017.05 2017 5
2 2017.06 2017 6
3 2017.07 2017 7
4 2017.08 2017 8
5 2017.09 2017 9
6 2017.10 2017 10
Answered By - not_speshal Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.