Home > database >  Extract rows from a df using names
Extract rows from a df using names

Time:10-13

I want to extract a bunch of well names from a large df. I have made a small example to show what I want to do. I want to extract the data for the wells listed in selected_wells from the df.

import pandas as pd

# assign data of lists.
data = {'Wellnames': ['Well1', 'Well2', 'Well3', 'Well4','Well5', 'Well6', 'Well7', 'Well8'], 
    'waterlevel': [20, 21, 19, 18,20, 21, 19, 18],
    'static': [10,12,14,12,9,12,18,10]}
# Create DataFrame.
df = pd.DataFrame(data)

selected_wells = ['Well2', 'Well5']

CodePudding user response:

Use isin:

>>> df[df['Wellnames'].isin(selected_wells)]
  Wellnames  waterlevel  static
1     Well2          21      12
4     Well5          20       9
>>> 
  • Related