Home > Software engineering >  Filtering by rows Pandas Daraframe Python
Filtering by rows Pandas Daraframe Python

Time:02-26

I want to filer the pandas dataframe where it filters out every other column out of the dataframe except the rows stated within the rows values. How would I be able to do that and get the Expected Output.

import pandas as pd 

data = pd.DataFrame({'Symbol': {0: 'ABNB', 1: 'DKNG', 2: 'EXPE', 3: 'MPNGF', 4: 'RDFN', 5: 'ROKU', 6: 'VIACA', 7: 'Z'},
'Number of Buy       s': {0: nan, 1: 2.0, 2: nan, 3: 1.0, 4: 2.0, 5: 1.0, 6: 1.0, 7: nan}, 
'Number of Sell      s': {0: 1.0, 1: nan, 2: 1.0, 3: nan, 4: nan, 5: nan, 6: nan, 7: 1.0}, 
'Gains/Losses': {0: 2106.0, 1: -1479.2, 2: 1863.18, 3: -1980.0, 4: -1687.7, 5: -1520.52, 6: -1282.4, 7: 1624.59}, 'Percentage change': {0: 0.0, 1: 2.0, 2: 0.0, 3: 0.0, 4: 1.5, 5: 0.0, 6: 0.0, 7: 0.0}})

rows = ['ABNB','DKNG','EXPE']

Expected Output:

enter image description here

CodePudding user response:

Use .inin()

data[data['Symbol'].isin(rows)]
  • Related