I have the following code:
print(df.drop(df[df['Quantity'] == 0].index).rename(columns={'Weight': 'Weight (oz.)'}))
I understand what query is trying to do, but I'm lost at why you need to add the ".index " portion?
What is.index doing in this particular code?
For context here is what the dataframe looks like:
I looked at the python documentation for dataframe index:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.index.html
but unfortunately it was too vague for me to make sense of it.
The DataFrame.index
is the index of each record in your dataframe. It is unique to each row even if two rows have the same data in each column. DataFrame.drop
takes the index: single label or list-like
and drops those rows that match the index.
So from the code above,
df[df['Quantity'] == 0]
gets the rows that has Quantity == 0
, df[df['Quantity'] == 0].index
gets the indexes of all rows that has the predicate, df.drop(df[df['Quantity'] == 0].index)
this drops all the indices that returned True for that predicate.
Hope this helps!
I checked df.drop()
's documentation. It says that it drops by index. This code first finds the items that has the quantity 0, but because drop()
works with indexes, it sends the items back to the dataframe and receive their indexes. That's index
.
https://pandas.pydata.org/pandas-docs/stable//reference/api/pandas.DataFrame.drop.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.