Use DataFrame.drop and pass it a Series of index labels:
In [65]: df
Out[65]:
one two
one 1 4
two 2 3
three 3 2
four 4 1
In [66]: df.drop(df.index[[1,3]])
Out[66]:
one two
one 1 4
three 3 2
Answer from tzelleke on Stack OverflowUse DataFrame.drop and pass it a Series of index labels:
In [65]: df
Out[65]:
one two
one 1 4
two 2 3
three 3 2
four 4 1
In [66]: df.drop(df.index[[1,3]])
Out[66]:
one two
one 1 4
three 3 2
Note that it may be important to use the "inplace" command when you want to do the drop in line.
df.drop(df.index[[1,3]], inplace=True)
Because your original question is not returning anything, this command should be used. http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.drop.html
python - Remove Row Index dataframe pandas - Stack Overflow
Removing rows from pandas dataframe efficiently?
Removing a row from a pandas dataframe by index location
indexing - How to delete a row in a Pandas DataFrame and relabel the index? - Stack Overflow
Videos
I hope this works :)
df.reset_index(inplace=True) # Resets the index, makes factor a column
df.drop("Factor",axis=1,inplace=True) # drop factor from axis 1 and make changes permanent by inplace=True
Try using:
df1.reset_index(drop=True)
This resets the index to the default integer index and removes the original one.
If you want to assign this change to original dataframe it is easier to use:
df1.reset_index(drop=True, inplace=True)
As it will edit the df1 dataframe without making a copy of it.
Has anyone here tried to drop a row from a pandas dataframe by index location rather than by the index? Is there anyway to do it similar to the drop attribute with the inplace option equal to True?
Like this question?:
https://stackoverflow.com/questions/46494583/drop-a-row-in-pandas-dataframe-base-on-integer-index-location
Use pandas.DataFrame.reset_index(), the option drop=True will do what you are looking for.
In [14]: df = pd.DataFrame(np.random.randn(5,4))
In [15]: df.ix[::3] = np.nan
In [16]: df
Out[16]:
0 1 2 3
0 NaN NaN NaN NaN
1 1.895803 0.532464 1.879883 -1.802606
2 0.078928 0.053323 0.672579 -1.188414
3 NaN NaN NaN NaN
4 -0.766554 -0.419646 -0.606505 -0.162188
In [17]: df = df.dropna()
In [18]: df.reset_index(drop=True)
Out[18]:
0 1 2 3
0 1.895803 0.532464 1.879883 -1.802606
1 0.078928 0.053323 0.672579 -1.188414
2 -0.766554 -0.419646 -0.606505 -0.162188
In addition to an accepted answer:
You should also use inplace=True as well:
df.reset_index(drop=True, inplace=True)