Choosing specific excel strings to parse in pandas / ipython?

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

I was able to learn how to select contiguous ranges using the iloc function as follows:

import pandas as pd

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

df.iloc[1:5]

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

+4
source share
2 answers

, :

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

, pyton 0-, .

+3
import pandas as pd

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

df:

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

Source: https://habr.com/ru/post/1611111/


All Articles