열 값을 통해 Pandas DataFrame을 필터링하는 것은 Python에서 정보를 사용하여 실행하는 동안 일반적인 작업입니다. 이를 달성하기 위해 다양한 방법과 기술을 사용할 수 있습니다. 열 값을 통해 Pandas DataFrame을 필터링하는 다양한 방법이 있습니다.
이 게시물에서는 Pandas Dataframe을 열 값으로 필터링하는 다양한 방법을 살펴보겠습니다. 먼저 데이터프레임을 생성해 보겠습니다.
파이썬3
# importing pandas> import> pandas as pd> > # declare a dictionary> record>=> {> >'Name'> : [>'Ankit'>,>'Swapnil'>,>'Aishwarya'>,> >'Priyanka'>,>'Shivangi'>,>'Shaurya'> ],> > >'Age'> : [>22>,>20>,>21>,>19>,>18>,>22>],> > >'Stream'> : [>'Math'>,>'Commerce'>,>'Science'>,> >'Math'>,>'Math'>,>'Science'>],> > >'Percentage'> : [>90>,>90>,>96>,>75>,>70>,>80>] }> > # create a dataframe> dataframe>=> pd.DataFrame(record,> >columns>=> [>'Name'>,>'Age'>,> >'Stream'>,>'Percentage'>])> # show the Dataframe> print>(>'Given Dataframe :
'>, dataframe)> |
>
>
산출:

'>', '=', '=', '<=', '!=' 연산자를 사용하여 특정 열 값을 기반으로 Pandas Dataframe의 행을 선택합니다.
예시 1: 다음을 사용하여 '백분율'이 75보다 큰 지정된 데이터 프레임에서 모든 행을 선택합니다. [ ] .
파이썬3
# selecting rows based on condition> rslt_df>=> dataframe[dataframe[>'Percentage'>]>>70>]> > print>(>'
Result dataframe :
'>, rslt_df)> |
>
>
산출:

예 2: 다음을 사용하여 '백분율'이 70보다 큰 지정된 데이터 프레임에서 모든 행을 선택합니다. 장소[ ] .
파이썬3
# selecting rows based on condition> rslt_df>=> dataframe.loc[dataframe[>'Percentage'>]>>70>]> > print>(>'
Result dataframe :
'>,> >rslt_df)> |
>
>
산출:

다음을 사용하여 열 값이 목록에 있는 Pandas Dataframe의 행을 선택합니다. 너() 데이터프레임의 메소드.
예시 1: 다음을 사용하여 옵션 목록에 'Stream'이 있는 지정된 데이터 프레임의 모든 행을 선택합니다. [ ] .
파이썬3
options>=> [>'Science'>,>'Commerce'>]> > # selecting rows based on condition> rslt_df>=> dataframe[dataframe[>'Stream'>].isin(options)]> > print>(>'
Result dataframe :
'>,> >rslt_df)> |
>
>
산출:

예 2: 다음을 사용하여 옵션 목록에 'Stream'이 있는 지정된 데이터 프레임의 모든 행을 선택합니다. 장소[ ] .
파이썬
options>=> [>'Science'>,>'Commerce'>]> > # selecting rows based on condition> rslt_df>=> dataframe.loc[dataframe[>'Stream'>].isin(options)]> > print>(>'
Result dataframe :
'>,> >rslt_df)> |
>
>
산출:

'&' 연산자를 사용하여 여러 열 조건을 기반으로 Pandas Dataframe의 행을 선택합니다.
예1: 'Age'가 22이고 옵션 목록에 'Stream'이 있는 지정된 데이터 프레임에서 모든 행을 선택합니다. [ ] .
파이썬3
options>=> [>'Commerce'> ,>'Science'>]> > # selecting rows based on condition> rslt_df>=> dataframe[(dataframe[>'Age'>]>=>=> 22>) &> >dataframe[>'Stream'>].isin(options)]> > print>(>'
Result dataframe :
'>,> >rslt_df)> |
>
>
산출:

예 2: 'Age'가 22이고 옵션 목록에 'Stream'이 있는 지정된 데이터 프레임에서 모든 행을 선택합니다. 장소[ ] .
파이썬3
엑셀로 근속기간 계산하기
options>=> [>'Commerce'>,>'Science'>]> > # selecting rows based on condition> rslt_df>=> dataframe.loc[(dataframe[>'Age'>]>=>=> 22>) &> >dataframe[>'Stream'>].isin(options)]> > print>(>'
Result dataframe :
'>,> >rslt_df)> |
>
>
산출:
