Modi per filtrare Pandas DataFrame in base ai valori delle colonne
Filtrare un DataFrame Pandas tramite valori di colonna è un'operazione comune durante l'esecuzione con informazioni in Python. È possibile utilizzare vari metodi e tecniche per raggiungere questo obiettivo. Ecco numerosi modi per filtrare un Pandas DataFrame tramite i valori delle colonne.
In questo post vedremo diversi modi per filtrare Pandas Dataframe in base ai valori delle colonne. Innanzitutto, creiamo un Dataframe:
Python3
# 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)> |
Produzione:
Selezione di righe di Pandas Dataframe in base a un particolare valore di colonna utilizzando l'operatore '>', '=', '=', ' <=', '!='.
Esempio 1: Selezionando tutte le righe dal Dataframe specificato in cui 'Percentuale' è maggiore di 75 utilizzando [] .
Python3
# selecting rows based on condition> rslt_df> => dataframe[dataframe[> 'Percentage'> ]>> 70> ]> > print> (> '
Result dataframe :
'> , rslt_df)> |
Produzione:
Esempio 2: Selezionando tutte le righe dal Dataframe specificato in cui 'Percentuale' è maggiore di 70 utilizzando posto [ ] .
Python3
# selecting rows based on condition> rslt_df> => dataframe.loc[dataframe[> 'Percentage'> ]>> 70> ]> > print> (> '
Result dataframe :
'> ,> > rslt_df)> |
Produzione:
Selezionando quelle righe di Pandas Dataframe il cui valore di colonna è presente nell'elenco utilizzando Voi() metodo del dataframe.
Esempio 1: Selezionando tutte le righe dal dataframe specificato in cui è presente 'Stream' nell'elenco delle opzioni utilizzando [] .
Python3
options> => [> 'Science'> ,> 'Commerce'> ]> > # selecting rows based on condition> rslt_df> => dataframe[dataframe[> 'Stream'> ].isin(options)]> > print> (> '
Result dataframe :
'> ,> > rslt_df)> |
Produzione:
Esempio 2: Selezionando tutte le righe dal dataframe specificato in cui è presente 'Stream' nell'elenco delle opzioni utilizzando posto [ ] .
Pitone
options> => [> 'Science'> ,> 'Commerce'> ]> > # selecting rows based on condition> rslt_df> => dataframe.loc[dataframe[> 'Stream'> ].isin(options)]> > print> (> '
Result dataframe :
'> ,> > rslt_df)> |
Produzione:
Selezione di righe di Pandas Dataframe in base a più condizioni di colonna utilizzando l'operatore '&'.
Esempio 1: Selezionando tutte le righe dal Dataframe specificato in cui 'Age' è uguale a 22 e 'Stream' è presente nell'elenco delle opzioni utilizzando [] .
Python3
options> => [> 'Commerce'> ,> 'Science'> ]> > # selecting rows based on condition> rslt_df> => dataframe[(dataframe[> 'Age'> ]> => => 22> ) &> > dataframe[> 'Stream'> ].isin(options)]> > print> (> '
Result dataframe :
'> ,> > rslt_df)> |
Produzione:
Esempio 2: Selezionando tutte le righe dal Dataframe specificato in cui 'Age' è uguale a 22 e 'Stream' è presente nell'elenco delle opzioni utilizzando posto [ ] .
Python3
options> => [> 'Commerce'> ,> 'Science'> ]> > # selecting rows based on condition> rslt_df> => dataframe.loc[(dataframe[> 'Age'> ]> => => 22> ) &> > dataframe[> 'Stream'> ].isin(options)]> > print> (> '
Result dataframe :
'> ,> > rslt_df)> |
Produzione: