首页 > 解决方案 > 如何删除熊猫(python)中特殊行下的所有行?

问题描述

如何在熊猫(Python)中删除具有一列“练习”的行下的所有行?

数据:

2021.08.16 19:37:15 146242975   XAUEUR  buy 0.02    1 517.04    1 517.19    1 519.54    2021.08.16 20:38:30 1 517.15    - 0.12  0.00     0.22
2021.08.16 19:37:15 146242976   XAUEUR  buy 0.02    1 517.04    1 517.19    1 522.04    2021.08.16 20:38:30 1 517.15    - 0.12  0.00     0.22
Exercises
2021.08.16 01:02:11 146037881   XAUUSD  buy 0.18 / 0.18 market  1 777.72    1 781.47    2021.08.16 01:02:11 filled      TP1
...

标签: pythonpandasdataframe

解决方案


df = pd.DataFrame({'num':[1,2,3,4,'Excercises',6,7,8]})
#First find the row index by filtering the column value
my_index = df.index[df['num'] == 'Exercises'].tolist()[0] # as you my find multiple match, take the first index found by [0]
#my_index  = 4
#Then slice the Dataframe and take values into new df
df_new = df[:my_index] # or if you want to exclude that row , then add +1 to my_index

推荐阅读