반응형
if
조건에서 특정 열이 실패하는 경우 반복 중에 현재 행을 삭제하고 싶습니다. using df.iterrows ()
.
전의.
for index, row in df:
if row['A'] == 0:
#remove/drop this row from the df
del df[index] #I tried this but it gives me an error
이것은 매우 쉬운 일일 수 있지만 여전히 방법을 알 수 없습니다. 귀하의 도움은 대단히 감사하겠습니다!
해결 방법
이것이 의사 코드인지 아닌지는 모르겠지만 이와 같은 행을 삭제할 수 없습니다. drop
할 수 있습니다.
In [425]:
df = pd.DataFrame({'a':np.random.randn(5), 'b':np.random.randn(5)})
df
Out[425]:
a b
0 -1.348112 0.583603
1 0.174836 1.211774
2 -2.054173 0.148201
3 -0.589193 -0.369813
4 -1.156423 -0.967516
In [426]:
for index, row in df.iterrows():
if row['a'] > 0:
df.drop(index, inplace=True)
In [427]:
df
Out[427]:
a b
0 -1.348112 0.583603
2 -2.054173 0.148201
3 -0.589193 -0.369813
4 -1.156423 -0.967516
해당 행을 필터링하려는 경우 부울 인덱싱을 수행 할 수 있습니다.
df[df['a'] <=0]
똑같은 것을 성취 할 것이다
참조 페이지 https://stackoverflow.com/questions/28876243
반응형
'파이썬' 카테고리의 다른 글
파이썬 Check if any item in Python list is None (but include zero) (0) | 2020.11.28 |
---|---|
파이썬에서 집합 작업에 대 업데이트 추가 (0) | 2020.11.28 |
파이썬 'module' has no attribute 'urlencode' (0) | 2020.11.28 |
파이썬 PDF 위에 이미지 배치 (0) | 2020.11.28 |
파이썬 PyQt5는 'QApplication'이름을 가져올 수 없습니다. (0) | 2020.11.28 |
댓글