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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With