Python Forum

Full Version: Dropping rows in a dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Hi,
I'm trying to delete a couple rows from a dataframe so I don't have unwanted text/values

import numpy as np
import pandas as pd
#Saving a copy of the original values
temp['Sales_Orig']=temp['Sales']
#Log-transform Sales
temp['Sales']=np.log(temp['Sales'])
temp.tail()
Output:
Month Sales Sales_Orig 102 1972-07 2.124165 8.365905 103 1972-08 1.981480 7.253470 104 1972-09 2.160883 8.678802 105 NaN NaN NaN 106 Perrin Freres monthly champagne sales millions... NaN NaN
I couldn't figure out how to directly delete those last 2 rows, so I tried copying to a new dataframe and leaving off the last couple rows:
new_df = temp[:105]
new_df['Sales']=np.log(new_df['Sales'])
new_df.tail()
...however here I got the following message
Output:
A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
I don't really care how they get deleted - I just want to know how to do it cleanly and consistently.
Any help is appreciated. Thanks!