Python Forum
Dropping rows in a dataframe
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dropping rows in a dataframe
#1

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!
Reply


Messages In This Thread
Dropping rows in a dataframe - by sobrio1 - Dec-03-2017, 11:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas Dataframe Filtering based on rows mvdlm 0 1,453 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  change dataframe header with 2 rows tonycat 2 2,011 Oct-29-2020, 01:41 AM
Last Post: tonycat
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 2,239 Aug-03-2020, 02:05 AM
Last Post: scidam
  How to add a few empty rows into a pandas dataframe python_newbie09 2 16,362 Sep-20-2019, 08:52 AM
Last Post: python_newbie09
  Dropping a column from pandas dataframe marco_ita 6 15,362 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  Dataframe Rows Sorting stranger14u 1 2,586 Dec-17-2018, 11:47 PM
Last Post: scidam
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 5,539 Oct-18-2018, 09:33 PM
Last Post: volcano63
  Add rows in dataframe arya_starc 1 2,388 Oct-03-2018, 08:02 AM
Last Post: volcano63
  Dropping all rows of multiple columns after the max of one cell Thunberd 2 2,977 Jun-01-2018, 10:18 PM
Last Post: Thunberd
  Get rows with same value from dataframe of particular columns angelwings 1 2,742 Apr-11-2018, 02:40 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020