为什么用 df['date'].astype(str).str[-2:] 转换的 "YYYY.MM" 日期会出现在 0 和 1 之间?
Why does an "YYYY.MM" date converted with df['date'].astype(str).str[-2:] come out between 0 and 1?
我有以下数据框:
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
我用以下方法生成了这个数据框:
australia_overview_clean['month'] = australia_overview_clean['date'].astype(str).str[-2:]
australia_overview_clean
但出于某种原因,我没有返回“10”月份,而是返回“0.1”。虽然它在数学上与 10 或 0.10 相同,但我不介意找到一种方法来代替 10。有什么办法可以解决这个问题吗?
您可以通过拆分“.
”从日期中获取您的年份和月份。
ljust
用于确保日期列在拆分前是7个字符长。
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
我有以下数据框:
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
我用以下方法生成了这个数据框:
australia_overview_clean['month'] = australia_overview_clean['date'].astype(str).str[-2:]
australia_overview_clean
但出于某种原因,我没有返回“10”月份,而是返回“0.1”。虽然它在数学上与 10 或 0.10 相同,但我不介意找到一种方法来代替 10。有什么办法可以解决这个问题吗?
您可以通过拆分“.
”从日期中获取您的年份和月份。
ljust
用于确保日期列在拆分前是7个字符长。
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