Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从一个日期时间格式到另一个日期时间格式的一列_Python - Fatal编程技术网

Python 从一个日期时间格式到另一个日期时间格式的一列

Python 从一个日期时间格式到另一个日期时间格式的一列,python,Python,我觉得更改日期时间格式很烦人 y_hat.head() 0 year 0 649.003 1960-12-31 1 649.003 1961-12-31 2 649.003 1962-12-31 3 649.003 1963-12-31 4 649.003 1964-12-31 我有一个专栏叫['year',,我想把它从'1960-12-31'改为'1960'。 我试过很多方法,比如 有人能为我和其他人提供一种方法吗?您可以转换列并提取: 或并提取列表

我觉得更改日期时间格式很烦人

y_hat.head()

    0       year
0   649.003 1960-12-31
1   649.003 1961-12-31
2   649.003 1962-12-31
3   649.003 1963-12-31
4   649.003 1964-12-31
我有一个专栏叫['year',,我想把它从'1960-12-31'改为'1960'。 我试过很多方法,比如 有人能为我和其他人提供一种方法吗?

您可以转换列并提取:

或并提取列表的第一个值,然后转换为整数:

df['year'] = df['year'].str.split('-').str[0].astype(int)

您可以转换列并提取:

或并提取列表的第一个值,然后转换为整数:

df['year'] = df['year'].str.split('-').str[0].astype(int)

print (df)
         0  year
0  649.003  1960
1  649.003  1961
2  649.003  1962
3  649.003  1963
4  649.003  1964