简体   繁体   English

哪个 pandas DataFrame 行在应用 function 时发出警告?

[英]Which pandas DataFrame row raised a warning with apply function?

I have a DataFrame in which each row is one observation and each column a parameter.我有一个 DataFrame ,其中每一行是一个观察值,每一列是一个参数。 I need to apply a function to each row, and in some of them it raises a warning but I can't find out which ones.我需要对每一行应用 function ,其中一些会warning ,但我不知道是哪些。 Example:例子:

result = df.apply(lambda x: func(x, args), axis=1)

If df has 1000 rows, 500 will raise an identical warning message from func , but there's no way for me to know which ones.如果df有 1000 行, 500 将从func引发相同的警告消息,但我无法知道哪些。

Is there a way to output the DataFrame's rows that raised the warning and save them to a list, for example?例如,有没有办法 output 引发警告的 DataFrame 行并将它们保存到列表中?

If this is for troubleshooting (vs production) you can use df.iterrows() to apply the function and print or log the row number, for example如果这是用于故障排除(相对于生产),您可以使用df.iterrows()应用 function 并打印或记录行号,例如

for n, row in enumerate(df.iterrows()):
    print(row)
    func(row, args)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM