반응형
이와 같은 데이터 프레임이 있습니다.
col1 col2
0 something1 something1
1 something2 something3
2 something1 something1
3 something2 something3
4 something1 something2
col1
또는 col2
에서 something1
이있는 모든 행을 필터링하려고합니다. 열에 조건 논리가 필요한 경우 df [df.col1 == 'something1']
을 사용하여 수행 할 수 있지만 여러 열로 수행 할 수있는 방법이 있습니까?
해결 방법
print ((df == 'something1').all(1))
0 True
1 False
2 True
3 False
4 False
dtype: bool
print (df[(df == 'something1').all(1)])
col1 col2
0 something1 something1
2 something1 something1
편집하다:
print (df)
col1 col2 col3
0 something1 something1 a
1 something2 something3 s
2 something1 something1 r
3 something2 something3 a
4 something1 something2 a
cols = df.columns[df.columns.isin(['col1','col2'])]
print (cols)
Index(['col1', 'col2'], dtype='object')
print (df[(df[cols] == 'something1').all(1)])
col1 col2 col3
0 something1 something1 a
2 something1 something1 r
참조 페이지 https://stackoverflow.com/questions/37663931
반응형
'파이썬' 카테고리의 다른 글
파이썬 A non-blocking read on a subprocess.PIPE in Python (0) | 2020.11.03 |
---|---|
파이썬 re.finditer와 re.findall의 다른 동작 (0) | 2020.11.03 |
파이썬 'double_scalars에서 잘못된 값이 발견되었습니다'경고, 아마도 numpy (0) | 2020.11.03 |
파이썬 What is the difference between 'SAME' and 'VALID' padding in tf.nn.max_pool of tensorflow? (0) | 2020.11.03 |
파이썬 Pandas는 문자열에서 숫자를 추출합니다. (0) | 2020.11.03 |
댓글