Julia T.
Julia T.

Reputation: 53

Selecting specific excel rows for analysis in pandas/ipython?

This question is probably quite elementary, but I am totally stuck here so I would appreciate any help: Is there a way to extract data for analysis from an excel file by selecting specific row numbers? For example, if I have an excel file with 30 rows, and I want to add up the values of row 5+10+21+27 ?

I only managed to learn how to select adjacent ranges with the iloc function like this:

import pandas as pd

df = pd.read_excel("example.xlsl")

df.iloc[1:5]

If this is not possible in Pandas, I would appreciate advice how to copy selected rows from a spreadsheet into a new spreadsheet via openpyxl, then I could just load the new worksheet into Pandas.

Upvotes: 5

Views: 8580

Answers (2)

ako
ako

Reputation: 3689

You can do like so, passing a list of indices:

df.iloc[[4,9,20,26]].sum()

Mind that pyton uses 0-indexing, so these indices are one below the desired row numbers.

Upvotes: 4

xiº
xiº

Reputation: 4687

import pandas as pd

df = pd.read_excel("example.xlsx")   
sum(df.data[i - 1] for i in [5, 10, 21, 27])

My df:

    data
0      1
1      2
2      3
3      4
4      5
...

Upvotes: 0

Related Questions