Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting specific rows from a pandas dataframe

Tags:

python

pandas

I just want to know if there is any function in pandas that selects specific rows based on index from a dataframe without having to write your own function.

For example: selecting rows with index [15:50] from a large dataframe.

I have written this function, but I would like to know if there is a shortcut.

def split_concat(data , first , last):
    data_out = pd.DataFrame()
    for i in range(first, last +1):
        data_split = data.loc[i]
        data_out = pd.concat([data_out,data_split],axis = 0)

    return data_out
like image 290
Naciif Soufiane Avatar asked Jun 19 '26 06:06

Naciif Soufiane


2 Answers

You could use either pandas.DataFrame.loc or pandas.DataFrame.iloc. See examples below.

import pandas as pd

d = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
     {'a': 100, 'b': 200, 'c': 300, 'd': 400},
     {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 },
     {'a': 1500, 'b': 2500, 'c': 3500, 'd': 4500}]

df = pd.DataFrame(d)

print(df)               # Print original dataframe
print(df.loc[1:2])      # Print rows with index 1 and 2, (method 1)
print(df.iloc[1:3])     # Print rows with index 1 and 2, (method 2)

Original dataframe: print(df) will print:

      a     b     c     d
0     1     2     3     4
1   100   200   300   400
2  1000  2000  3000  4000
3  1500  2500  3500  4500

And print(df.loc[1:2]) for index selection by label:

      a     b     c     d
1   100   200   300   400
2  1000  2000  3000  4000

And print(df.iloc[1:3]) for row selection by integer. As mentioned by ALollz, rows are treated as numbers from 0 to len(df):

      a     b     c     d
1   100   200   300   400
2  1000  2000  3000  4000

A rule of thumb could be:

  • Use .loc when you want to refer to the actual value of the index, being a string or integer.

  • Use .iloc when you want to refer to the underlying row number which always ranges from 0 to len(df).

Note that the end value of the slice in .loc is included. This is not the case for .iloc, and for Python slices in general.

Pandas in general

Pandas has 'easy' ways of doing all sorts of stuff like this. If you have a problem that you think is common for manipulation of tabular data, try searching for pandas ways of getting it done before inventing it yourself. Pandas will almost always have a syntactically concise and computationally faster way of doing things than what we can write ourselves.

like image 108
Tim Skov Jacobsen Avatar answered Jun 21 '26 20:06

Tim Skov Jacobsen


Use this:

rowData = your_df.loc[ 'index' , : ]
like image 40
SdahlSean Avatar answered Jun 21 '26 20:06

SdahlSean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!