我有一些时间序列数据,看起来像这样:
1998-01-02 09:30:00,0.4298,0.4337,0.4258,0.4317,6426369
1999-01-02 09:45:00,0.4317,0.4337,0.4258,0.4298,10589080
2000-01-02 10:00:00,0.4298,0.4337,0.4278,0.4337,9507980
2001-01-02 10:15:00,0.4337,0.4416,0.4298,0.4416,13639022
我想要的是一个年份列表。
years = list['1998'.'1999','2000','2001']
因此,我可以使用该列表来了解我可以在该数据框中查询哪些年份。并非所有的数据框中都有相同的年份。
data = pd.read_csv(str(inFileName), index_col=0, parse_dates=True, header=None)
#data.iloc[:, 0]
print(pd.DatetimeIndex(data.iloc[:, 0]).year)
#print(data.iloc[:, 0])
#years = list(data.index)
#print(years)
for x in years:
我正在尝试很多方法,但都没有成功。能否有人向我解释如何解决这样的问题?
编辑1:经过一些建议,我正在尝试以下操作:
data = pd.read_csv(str(inFileName), parse_dates=[0], header=None)
data.iloc[:, 0] = pd.to_datetime(data.iloc[:, 0])
data['year'] = data.iloc[:, 0].apply(lambda x: x.year)
year_list = data['year'].unique().tolist()
print(year_list)
for x in year_list:
newDF = data[x]
newDF.head()
print(newDF.head(5))
我得到了列表:[2017, 2018, 2019]
但是我无法从该列表创建新的数据框。我想为列表中的每个值创建一个新的数据框。我遇到了错误:
[2017, 2018, 2019]
Traceback (most recent call last):
File "/home/jason/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 2017
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./massageSM.py", line 123, in <module>
main(sys.argv[1:])
File "./massageSM.py", line 33, in main
newDF = data[x]
File "/home/jason/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py", line 2688, in __getitem__
return self._getitem_column(key)
File "/home/jason/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py", line 2695, in _getitem_column
return self._get_item_cache(key)
File "/home/jason/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py", line 2489, in _get_item_cache
values = self._data.get(item)
File "/home/jason/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/internals.py", line 4115, in get
loc = self.items.get_loc(item)
File "/home/jason/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 2017
编辑2
我正在使用这个:
data = pd.read_csv("RHE.SM", parse_dates=[0], header=None)
data.iloc[:, 0] = pd.to_datetime(data.iloc[:, 0])
data['year'] = data.iloc[:, 0].apply(lambda x: x.year)
year_list = data['year'].unique().tolist()
print(year_list)
for x in year_list:
df = pd.DataFrame({'years':year_list})
print(df.head(5))
并且它会产生输出:
[2017, 2018, 2019]
years
0 2017
1 2018
2 2019
years
0 2017
1 2018
2 2019
years
0 2017
1 2018
2 2019
但是我想要创建以下内容: 只包含2017的数据框 只包含2018的数据框 只包含2019的数据框
但是我不能硬编码,因为其他文件可能不包含相同的年份。我需要制作一个可用年份列表,并对其进行迭代。
编辑3:
我也尝试过:
data = pd.read_csv("RHE.SM", header=None, parse_dates=[0])
year_list = data[0].dt.year.unique().tolist()
print(year_list)
data.index = pd.DatetimeIndex(data[0])
print(type(data.index))
print(data.index)
for x in year_list:
print(x)
newDF = data[x]
#newDF.head()
#print(newDF.head(5))
我得到了以下输出,一开始很好,但后来创建 newDF 时出现错误。
[2017, 2018, 2019]
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
DatetimeIndex(['2017-10-02 10:15:00', '2017-10-02 10:30:00',
'2017-10-02 10:45:00', '2017-10-02 11:00:00',
'2017-10-02 11:15:00', '2017-10-02 11:30:00',
'2017-10-02 11:45:00', '2017-10-02 12:00:00',
'2017-10-02 12:15:00', '2017-10-02 12:30:00',
...
'2019-01-03 14:45:00', '2019-01-03 15:00:00',
'2019-01-03 15:15:00', '2019-01-03 15:30:00',
'2019-01-03 15:45:00', '2019-01-03 16:00:00',
'2019-01-03 16:30:00', '2019-01-03 16:45:00',
'2019-01-03 17:15:00', '2019-01-03 18:30:00'],
dtype='datetime64[ns]', name=0, length=8685, freq=None)
2017
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3077 try:
-> 3078 return self._engine.get_loc(key)
3079 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 2017
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-19-f31493ccbf2a> in <module>
9 for x in year_list:
10 print(x)
---> 11 newDF = data[x]
12 #newDF.head()
13
~/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
2686 return self._getitem_multilevel(key)
2687 else:
-> 2688 return self._getitem_column(key)
2689
2690 def _getitem_column(self, key):
~/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in _getitem_column(self, key)
2693 # get column
2694 if self.columns.is_unique:
-> 2695 return self._get_item_cache(key)
2696
2697 # duplicate columns & possible reduce dimensionality
~/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
2487 res = cache.get(item)
2488 if res is None:
-> 2489 values = self._data.get(item)
2490 res = self._box_item_values(item, values)
2491 cache[item] = res
~/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/internals.py in get(self, item, fastpath)
4113
4114 if not isna(item):
-> 4115 loc = self.items.get_loc(item)
4116 else:
4117 indexer = np.arange(len(self.items))[isna(self.items)]
~/Applications/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3078 return self._engine.get_loc(key)
3079 except KeyError:
-> 3080 return self._engine.get_loc(self._maybe_cast_indexer(key))
3081
3082 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 2017
df = pd.DataFrame({'years':year_list})
,并告诉我是否解决了问题。 - Nathan Hellingayear_list
中的每个单独项目创建一个新的数据框,您可以尝试我刚刚添加到答案中的代码。 - Nathan Hellinga