English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Pandas 주의사항 및 트랩
부울 연산자 if나 when, or나 or not을 사용하여 특정 내용을 부울로 변환하려고 시도할 때, 때로는 오류가 발생합니다. 오류가 어떻게 발생했는지는 아직 명확하지 않습니다. Pandas는 ValueError 예외를 제안합니다.
import pandas as pd if pd.Series([False, True, False]): print 'I am True'
출력 결과는 다음과 같습니다:
ValueError: The truth value of a Series is ambiguous. use a.empty, a.bool(), a.item(), a.any(), a.all().
이 경우, 어떻게 처리해야 할지 명확하지 않습니다. 이 오류는 None이나 그 중 하나를 사용했음을 암시합니다.
import pandas as pd if pd.Series([False, True, False]).any(): print("I am any")
출력 결과는 다음과 같습니다:
I am any
부울 컨텍스트에서 단일 요소 Pandas 객체를 평가하려면 .bool() 메서드를 사용하십시오-
import pandas as pd print pd.Series([True]).bool()
출력 결과는 다음과 같습니다:
True
==과 !과 같은 비트 부울 연산자는 부울 시리즈를 반환하며, 이는 거의 항상 필요합니다.
import pandas as pd s = pd.Series(range(5)) print s==4
출력 결과는 다음과 같습니다:
0 False 1 False 2 False 3 False 4 True dtype: bool
이는 전달된 값 시퀀스에 각 요소가 완전히 포함되는지 표시하는 부울 시리즈를 반환합니다.
import pandas as pd s = pd.Series(list('abc')) s = s.isin(['a', 'c', 'e']) print s
출력 결과는 다음과 같습니다:
0 True 1 False 2 True dtype: bool
많은 사용자들이 ix 인덱싱 기능을 데이터 선택의 간결한 방법으로 사용하게 됩니다:
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(6, 4), columns=['one', 'two', 'three', ,'four'],index=list('abcdef')) print df print df.ix[['b', 'c', 'e']]
출력 결과는 다음과 같습니다:
one two three four a -1.582025 1.335773 0.961417 -1.272084 b 1.461512 0.111372 -0.072225 0.553058 c -1.240671 0.762185 1.511936 -0.630920 d -2.380648 -0.029981 0.196489 0.531714 e 1.846746 0.148149 0.275398 -0.244559 f -1.842662 -0.933195 2.303949 0.677641 one two three four b 1.461512 0.111372 -0.072225 0.553058 c -1.240671 0.762185 1.511936 -0.630920 e 1.846746 0.148149 0.275398 -0.244559
물론, 이 경우 reindex 메서드를 사용하는 것과 동일합니다:
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(6, 4), columns=['one', 'two', 'three', ,'four'],index=list('abcdef')) print df print df.reindex(['b', 'c', 'e'])
출력 결과는 다음과 같습니다:
one two three four a 1.639081 1.369838 0.261287 -1.662003 b -0.173359 0.242447 -0.494384 0.346882 c -0.106411 0.623568 0.282401 -0.916361 d -1.078791 -0.612607 -0.897289 -1.146893 e 0.465215 1.552873 -1.841959 0.329404 f 0.966022 -0.190077 1.324247 0.678064 one two three four b -0.173359 0.242447 -0.494384 0.346882 c -0.106411 0.623568 0.282401 -0.916361 e 0.465215 1.552873 -1.841959 0.329404
사람들은 ix와 reindex가 이에 기반하여100%의 동일성을 가지고 있습니다. 정수 인덱스의 경우를 제외하고는 모두 이와 같습니다. 예를 들어, 위의 작업은 다음과 같이 대체 가능합니다:
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(6, 4), columns=['one', 'two', 'three', ,'four'],index=list('abcdef')) print df print df.ix[[1, 2, 4]) print df.reindex([1, 2, 4])
출력 결과는 다음과 같습니다:
one two three four a -1.015695 -0.553847 1.106235 -0.784460 b -0.527398 -0.518198 -0.710546 -0.512036 c -0.842803 -1.050374 0.787146 0.205147 d -1.238016 -0.749554 -0.547470 -0.029045 e -0.056788 1.063999 -0.767220 0.212476 f 1.139714 0.036159 0.201912 0.710119 one two three four b -0.527398 -0.518198 -0.710546 -0.512036 c -0.842803 -1.050374 0.787146 0.205147 e -0.056788 1.063999 -0.767220 0.212476 one two three four 1 NaN NaN NaN NaN 2 NaN NaN NaN NaN 4 NaN NaN NaN NaN
중요한 것은, 재인덱싱은 오직 엄격한 태그 인덱스일 뿐입니다. 인덱스가 정수와 문자와 같은 값을 포함하고 있을 때 오류가 발생할 수 있으며, 이는 예상치 못한 결과를 초래할 수 있습니다.