[英]Pandas: Creating a loop that selects rows from excel file
I am trying to read a certain excel file that contains specific data and modify them as well.我正在尝试读取某个包含特定数据的 excel 文件并对其进行修改。 Although I am not able to create a loop that reads the first 3 rows and then reads rows based on a pattern.
虽然我无法创建一个循环来读取前 3 行,然后根据模式读取行。 I would like to read initially row numbers 1,2,3 then 10,11,12 and so on.
我想先阅读行号 1、2、3,然后是 10、11、12,依此类推。 I would appreciate any help.
我将不胜感激任何帮助。 Thanks in advance.
提前致谢。 Have a nice day!
祝你今天过得愉快!
import numpy
import pandas as pd
df = pd.read_excel('My Excel File.xlsx')
for i, row in df.iterrows(0,156,3):
dfi = df.iloc[(i)].mean()
df1 =1/(numpy.log10(dfi))
print(df1)
Pandas DataFrame.read_excel
method has a parameter called skiprows
, it can receive multiple types of data, list
, int
or callable
. Pandas
DataFrame.read_excel
方法有一个参数叫做skiprows
,它可以接收多种类型的数据, list
, int
或callable
。 The latter being a function that would return True
if the function should be skipped and False
otherwise ( documentation here )后者是 function 如果应该跳过 function 将返回
True
否则返回False
( 文档here )
In your case you did not describe the pattern on how rows are skipped, you said row 1,2,3 then 10,11,12.在您的情况下,您没有描述如何跳过行的模式,您说的是第 1、2、3 行,然后是 10、11、12 行。 Assuming that you want to skip these specifics rows you can do:
假设您想跳过这些具体行,您可以执行以下操作:
df = pd.read_excel('My Excel File.xlsx', skiprows=lambda x: (x+1) in [1,2,3,10,11,12])
Here I added 1
to x
, x
represents the index ( ~ line number) starting at 0.这里我给
x
加了1
, x
代表从 0 开始的索引(~ 行号)。
If I understand your question correctly, you want to do something with every n consecutive rows every m rows.如果我正确理解您的问题,您希望每 m 行每 n 行连续执行一次操作。 Why not use a condition on the iterable?
为什么不在可迭代对象上使用条件?
import numpy
import pandas as pd
n = 3
m = 9
df = pd.read_excel('My Excel File.xlsx')
for idx in range(1, len(df.index)-n, m):
wanted_df_slice = df.iloc[[idx:idx+n-1]]
do_stuff(wanted_df_slice)
Edit: @arhr 's approach seems more elegant, as it uses a parameter that is meant precisely for that, and avoids adding a for loop.编辑:@arhr 的方法似乎更优雅,因为它使用了一个正是为此而设计的参数,并且避免了添加 for 循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.