Home > Software design >  how to select particular rows and columns without hardcoding in pandas python
how to select particular rows and columns without hardcoding in pandas python

Time:07-05

I want to select name and score from index 1 to 3 and store it to a dataframe. Can we extract that without hardcoding by giving a start word and end word.

import pandas as pd

name_dict = {
            'Name': ['a','b','c','d', 'e'],
            'Score': ['0.90(subject to approval)',80,95,20,10]
          }

df = pd.DataFrame(name_dict)

print (df)
df.set_index('Name').loc['a', 'Score']

enter image description here

CodePudding user response:

You can use iloc:

>>> df.iloc[1:4]
  Name Score
1    b    80
2    c    95
3    d    20
  • Related