반응형
pandas.Series
에 정수를 추가하고 싶습니다.
내 코드는 다음과 같습니다.
import pandas as pd
input = pd.Series([1,2,3,4,5])
input.append(6)
이것을 실행하면 다음 오류가 발생합니다.
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
f.append(6)
File "C:\Python33\lib\site-packages\pandas\core\series.py", line 2047, in append
verify_integrity=verify_integrity)
File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 878, in concat
verify_integrity=verify_integrity)
File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 954, in __init__
self.new_axes = self._get_new_axes()
File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 1146, in _get_new_axes
concat_axis = self._get_concat_axis()
File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 1163, in _get_concat_axis
indexes = [x.index for x in self.objs]
File "C:\Python33\lib\site-packages\pandas\tools\merge.py", line 1163, in <listcomp>
indexes = [x.index for x in self.objs]
AttributeError: 'int' object has no attribute 'index'
어떻게 고칠 수 있습니까?
해결 방법
추가 된 항목을 시리즈
로 변환 :
>>> ds = pd.Series([1,2,3,4,5])
>>> ds.append(pd.Series([6]))
0 1
1 2
2 3
3 4
4 5
0 6
dtype: int64
또는 DataFrame
사용 :
>>> df = pd.DataFrame(ds)
>>> df.append([6], ignore_index=True)
0
0 1
1 2
2 3
3 4
4 5
5 6
인덱스에 공백이없는 경우 마지막 옵션,
>>> ds.set_value(max(ds.index) + 1, 6)
0 1
1 2
2 3
3 4
4 5
5 6
dtype: int64
그리고 마지막 수단으로 numpy를 사용할 수 있습니다.
>>> import numpy as np
>>> pd.Series(np.concatenate((ds.values, [6])))
참조 페이지 https://stackoverflow.com/questions/20441980
반응형
'파이썬' 카테고리의 다른 글
파이썬 kivy에서 버튼 또는 레이블 텍스트 색상 변경 (0) | 2020.12.27 |
---|---|
파이썬 +에 대해 지원되지 않는 피연산자 유형 : 'int'및 'str' (0) | 2020.12.27 |
파이썬 Python PIL NameError 전역 이름 이미지가 정의되지 않았습니다. (0) | 2020.12.27 |
파이썬 How to use 2to3 properly for python? (0) | 2020.12.27 |
파이썬은 튜플을 배열로 변환 (0) | 2020.12.27 |
댓글