Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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 使用csv输入处理日期时间_Python_Pandas - Fatal编程技术网

Python 使用csv输入处理日期时间

Python 使用csv输入处理日期时间,python,pandas,Python,Pandas,我有一个csv文件,里面有这样的数据。 我是这样读的: Jan-17 Feb-17 Mar-17 Fixed 20.0 30.5 35.5 Variable 11.5 20.5 30.5 我希望从索引中提取月份: 但是,如果我做df.index.month,这就不起作用了 甚至连pd.to_datetime()都失败了。在这种情况下,有没有办法从索引中提取月份?您需要指定日期时间的格式: df = pd.read_

我有一个csv文件,里面有这样的数据。 我是这样读的:

           Jan-17   Feb-17   Mar-17
Fixed      20.0      30.5     35.5
Variable   11.5      20.5     30.5
我希望从索引中提取月份: 但是,如果我做df.index.month,这就不起作用了
甚至连pd.to_datetime()都失败了。在这种情况下,有没有办法从索引中提取月份?

您需要指定日期时间的格式:

df = pd.read_csv('data.csv', index_col= 0)
df = df.T
这将输出

pd.to_datetime(df.index,format='%b-%d')
默认年份为1900年。您可以使用
.month
.day
datetime
对象中选择月份和日期,而不使用
.dt
。例如:

DatetimeIndex(['1900-01-17', '1900-02-17', '1900-03-17'], dtype='datetime64[ns]', name='d', freq=None)
输出

pd.to_datetime(df.index,format='%b-%d').month

啊,我明白了,那么使用
格式=“%b-%y”
。格式字符可在以下位置找到:
Int64Index([1, 2, 3], dtype='int64', name='d')