Feb-25-2022, 02:39 PM
(This post was last modified: Feb-25-2022, 02:39 PM by deanhystad.)
Indexing in pandas is not like indexing a list. Deleting a row does not change the index of the trailing rows. In your example df_new doesn't have an index[0].
You filtered out index[0] when when creating df_new. df_new starts at index[1]
You can reset the index using reset_index()
https://pandas.pydata.org/pandas-docs/st...index.html
You filtered out index[0] when when creating df_new. df_new starts at index[1]
You can reset the index using reset_index()
https://pandas.pydata.org/pandas-docs/st...index.html
import pandas as pd data = {'x': [0,1, 2,3,4,5,6,7,8], 'y': [60, 23, 24,42,56,10,20,25,13], 'comment': ["false", "true","true","true","true","true","true","true","true"], } df = pd.DataFrame(data) df_new = df.loc[df['comment'] == "true"] print(df_new) df_new = df_new.reset_index(drop=True) print(df_new) df_new = df_new.drop(0) print(df_new)