unitown=pd.merge(Q1(),Q5(),how='inner',left_on=['State','RegionName'],right_index=True)
두 개의 데이터 프레임을 인덱스 'State'및 'RegionName'과 병합 한 후 unitown이라는 새 데이터 프레임을 만들었습니다. 다음은 unitown의 모습입니다. 여기에 이미지 설명을 입력하세요.
그림에서 볼 수 있듯이 연도 및 분기 형식으로 이름이 지정된 열이 있습니다. 그러나 시도 unitown['2000Q1']
하면 다음과 같은 오류가 발생합니다.
2798 if self.columns.nlevels > 1:
2799 return self._getitem_multilevel(key)
-> 2800 indexer = self.columns.get_loc(key)
2801 if is_integer(indexer):
2802 indexer = [indexer]
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2646 return self._engine.get_loc(key)
2647 except KeyError:
-> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key))
2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2650 if indexer.ndim > 1 or indexer.size > 1:
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.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: '2000Q1'*
나는 시도 unitown.columns.tolist()
했고 아래는 출력의 일부입니다.
['State',
'RegionName',
Period('2000Q1', 'Q-DEC'),
Period('2000Q2', 'Q-DEC'),
Period('2000Q3', 'Q-DEC'),
Period('2000Q4', 'Q-DEC'),
Period('2001Q1', 'Q-DEC'),
Period('2001Q2', 'Q-DEC'),
Period('2001Q3', 'Q-DEC'),
Period('2001Q4', 'Q-DEC'),
Period('2002Q1', 'Q-DEC'),
Period('2002Q2', 'Q-DEC'),
Period('2002Q3', 'Q-DEC'),
'2000Q1'이 분명히 열 이름 중 하나임을 감안할 때 그러한 오류가 발생하는 이유를 모르겠습니다. 누구든지 이것에 대해 나를 도울 수 있습니까? 감사합니다!
이렇게하면 문제가 해결됩니다.
df.columns = [str(col) for col in df.columns]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다