Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting specific excel rows for analysis in pandas/ipython?

Tags:

python

pandas

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.

like image 201
Julia T. Avatar asked Sep 26 '22 12:09

Julia T.


People also ask

How view specific rows from pandas excel?

Use pandas. read_excel() function to read excel sheet into pandas DataFrame, by default it loads the first sheet from the excel file and parses the first row as a DataFrame column name.

How can I get specific rows and columns in pandas?

To select a particular number of rows and columns, you can do the following using . loc . To select a single value from the DataFrame, you can do the following. You can use slicing to select a particular column.


1 Answers

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.

like image 138
ako Avatar answered Sep 30 '22 07:09

ako