Python 如何在 df.iterrows() 期间删除 pandas 数据框中的当前行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28876243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete the current row in pandas dataframe during df.iterrows()
提问by JCm
I would like to delete the current row during iteration - using df.iterrows()
, if it its certain column fails on my if
condition.
我想在迭代期间删除当前行 - using df.iterrows()
,如果它的某些列在我的if
条件下失败。
ex.
前任。
for index, row in df:
if row['A'] == 0:
#remove/drop this row from the df
del df[index] #I tried this but it gives me an error
This might be a very easy one, but i still can't figure out how to do it. Your help will be very much appreciated!
这可能是一个非常简单的方法,但我仍然不知道如何去做。您的帮助将不胜感激!
采纳答案by EdChum
I don't know if this is pseudo code or not but you can't delete a row like this, you can drop
it:
我不知道这是否是伪代码,但你不能像这样删除一行,你可以drop
:
In [425]:
df = pd.DataFrame({'a':np.random.randn(5), 'b':np.random.randn(5)})
df
Out[425]:
a b
0 -1.348112 0.583603
1 0.174836 1.211774
2 -2.054173 0.148201
3 -0.589193 -0.369813
4 -1.156423 -0.967516
In [426]:
for index, row in df.iterrows():
if row['a'] > 0:
df.drop(index, inplace=True)
In [427]:
df
Out[427]:
a b
0 -1.348112 0.583603
2 -2.054173 0.148201
3 -0.589193 -0.369813
4 -1.156423 -0.967516
if you just want to filter those rows out you can perform boolean indexing:
如果您只想过滤掉这些行,您可以执行布尔索引:
df[df['a'] <=0]
would achieve the same thing
会达到同样的目的
回答by abartt
I tried @EdChumsolution with a custom pandas.DataFrame
, but I did not get it working as an error was raising: KeyError: '[78] not found in axis'
. So on, if you got the same error, it can be fixed dropping the index of the dataframe on the specified index on each .iterrows()iteration.
我尝试使用自定义的@EdChum解决方案pandas.DataFrame
,但我没有让它工作,因为一个错误正在引发:KeyError: '[78] not found in axis'
。因此,如果您遇到相同的错误,则可以修复在每次.iterrows()迭代时删除指定索引上的数据帧索引。
The dataframe used was retrieved from investpywhich contains all the equities/stock data indexed in Investing.com, and the print function is the one implemented in pprint. Anyways, this is the piece of code to get it working:
所使用的数据框是从包含在Investing.com 中索引的所有股票/股票数据的investpy中检索的,并且打印功能是在pprint 中实现的功能。无论如何,这是让它工作的一段代码:
In [1]:
import investpy
from pprint import pprint
In [2]:
df = investpy.get_equities()
pprint(df.head())
Out [2]:
country name full_name \
0 argentina Tenaris Tenaris
1 argentina PETROBRAS ON Petroleo Brasileiro - Petrobras
2 argentina GP Fin Galicia Grupo Financiero Galicia B
3 argentina Ternium Argentina Ternium Argentina Sociedad Anónima
4 argentina Pampa Energía Pampa Energía S.A.
tag isin id currency
0 tenaris?cid=13302 LU0156801721 13302 ARS
1 petrobras-on?cid=13303 BRPETRACNOR9 13303 ARS
2 gp-fin-galicia ARP495251018 13304 ARS
3 siderar ARSIDE010029 13305 ARS
4 pampa-energia ARP432631215 13306 ARS
In [3]:
pprint(df[df['tag'] == 'koninklijke-philips-electronics'])
Out [3]:
country name full_name \
78 argentina Koninklijke Philips DRC Koninklijke Philips NV DRC
tag isin id currency
78 koninklijke-philips-electronics ARDEUT110558 30044 ARS
In [4]:
for index, row in df.iterrows():
if row['tag'] == 'koninklijke-philips-electronics':
df.drop(df.index[index], inplace=True)
In [5]:
pprint(df[df['tag'] == 'koninklijke-philips-electronics'])
Out [5]:
Empty DataFrame
Columns: [country, name, full_name, tag, isin, id, currency]
Index: []
Hope this helped someone! Also thank you anyways for the original answer @EdChum!
希望这对某人有所帮助!无论如何也感谢您的原始答案@EdChum!