将日期列分割为两个



我有以下数据框架:

<表类> 日期 风(°) 风(kt) 临时(C°) 湿度(%) 电流(°) 电流(kt) stemp (C°) sea_temp_diff wind_distance_diff wind_speed_diff temp_diff humidity_diff current_distance_diff current_speed_diff tbody><<tr>8 12018175.00000016.33333325.50000082.50000060.0000000.10000025.400000-1.06666723.333333-0.500000-0.333333-12.000000160.0000006.666667 e-029 12019180.00000017.00000023.34482879.724138230.0000000.10000023.827586-0.37931022.0689661.0689660.827586-7.275862315.1724143.449034 e + 0212020365.000000208.65384624.19230879.346154355.769231192.50000024.730769574.6538461121.9230771151.1538461149.346154-19.5384621500.0000001.538454 e + 0314 22019530.357143372.96428623.96428681.9642861270.7142861071.560714735.642857-533.642857-327.500000-356.8928571.857143-10.321429-873.571429-8.928107 e + 0215 22020216.55172412.68965524.51724181.137931288.275862172.565517196.827586-171.379310-8.9655173.7241381.413793-7.137931-105.517241-1.722724 e + 0232019323.225806174.70967725.22580680.741935260.000000161.45161325.709677480.709677486.451613483.9677420.387097153.1935481044.5161299.677065 e + 0232020351.333333178.56666725.53333378.800000427.666667166.66666726.600000165.533333-141.000000-165.766667166.633333158.9333338.3333331.500000 e-0118 42017180.00000014.00000027.0000005000.000000200.0000000.40000025.4000002.60000020.000000-4.0000000.0000000.000000-90.000000-1.000000 e-0119 42019694.230769589.76923124.03846269.461538681.153846577.04615426.884615-1.34615437.307692-1.6923081.5000004.76923198.8461541.538462 e-0142020306.666667180.06666724.73333375.166667427.666667166.66666726.800000165.066667205.333333165.2000001.100000-4.066667360.3333333.334233 e + 0221 52017146.33333311.96666722.9000005000.000000116.3333330.41000026.066667-1.5533338.6666670.833333-0.7666670.00000095.000000-1.300000 e-0122 52019107.74193512.32258123.41935563.032258129.3548390.33225825.935484-1.77419414.8387100.096774-0.612903-14.451613130.967742

From yourDataFrame:

>>> df = pd.DataFrame({'id': [1, 2, 3, 4], 
...                    'date': ['1 42018', '12 32019', '8 112020', '23 42021']}, 
...                   index = [0, 1, 2, 3]) 
>>> df
id   date
0   1   1 42018
1   2   12 32019
2   3   8 112020
3   4   23 42021

我们可以对列进行split以获得day的第一个值,如下所示:

>>> df['day'] = df['date'].str.split(' ', expand=True)[0]
>>> df
id  date        day
0   1   1 42018     1
1   2   12 32019    12
2   3   8 112020    8
3   4   23 42021    23

并从年份列date中获取最后4位数字,以获得预期结果:

>>> df['year'] = df['date'].str[-4:].astype(int)
>>> df
id  date        day year
0   1   1 42018     1   2018
1   2   12 32019    12  2019
2   3   8 112020    8   2020
3   4   23 42021    23  2021

奖励:正如评论中所问的,你甚至可以用同样的原则得到这个月:

>>> df['month'] = df['date'].str.split(' ', expand=True)[1].str[:-4].astype(int)
>>> df
id  date        day year    month
0   1   1 42018     1   2018    4
1   2   12 32019    12  2019    3
2   3   8 112020    8   2020    11
3   4   23 42021    23  2021    4

相关内容

  • 没有找到相关文章

最新更新