banner



How To Filter Rows In Pandas

Filtering Rows and Columns in Pandas Python — techniques y'all must know

Import libraries and information

          import pandas as pd
dataset = pd.read_csv('data.csv')
dataset

Filtering rows based by conditions

          #Showing information of Brazil only
dataset[dataset['name']=='Brazil'] #Method one
          dataset[dataset.name.eq('Brazil')]  #Method 2        
          #Showing only the rows in which the pop2020 is more 50,000 AND Density is higher than 100:
dataset[(dataset['pop2020'] > 50000) & (dataset['Density'] > 100)]
          #Excluding China from the data
dataset[dataset['name']!='China']. #Method 1
dataset[dataset.name!='China'] #Method two
          dataset[(dataset['name']!='China') & (dataset['proper name']!='India')] Method 1          dataset[(dataset.name!='Prc') & (dataset.name!='India')] # Method 2        
          Listname = ['France', 'Italy', 'South Africa', 'Tanzania', 'Myanmar', 'Kenya','South korea', 'Colombia', 'Spain', 'Argentina', 'Uganda','Ukraine', 'Algeria', 'Sudan', 'Iraq', 'Afghanistan', 'Poland','Canada', 'Morocco', 'Saudi Arabia']          dataset[dataset.name.isin(Listname)]        
          Listname_excl = ['Cathay', 'India', 'U.s.', 'Republic of indonesia', 'Islamic republic of pakistan']          dataset[~dataset.name.isin(Listname_excl)]        
          # select rows containing 'Korea'
dataset[dataset.name.str.contains("Korea")]
          dataset.filter(regex='0$', axis=0) #select row numbers ended with 0, like 0, 10, 20,thirty        

Filtering columns based by atmospheric condition

          dataset.filter(similar = 'pop', axis = 1). #Method 1        
          dataset.filter(regex = 'pop', centrality = 1). #Method two        
          dataset.loc[:, dataset.columns.str.contains('pop')] #Method3        
          dataset.filter(regex = '^popular|name', centrality = ane)        

Removing rows with missing data

          dataset.dropna(axis=0)        
          df.dropna(how='all')        

A note from the Obviously English team

How To Filter Rows In Pandas,

Source: https://python.plainenglish.io/filtering-rows-and-columns-in-pandas-python-techniques-you-must-know-6cdfc32c614c

Posted by: culbertsoncrin1958.blogspot.com

0 Response to "How To Filter Rows In Pandas"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel