-1
 **Index   date            colum1       column2**
      0       20200705        a              1.1%
      1       20200706        b              78%
      2       20200707        f              10%
      3       20200707        g              59%
      4       20200708        a              69%

Consider the name of the above data frame as 'df'. I want to delete the data for the particular date I tired the options below

dates =['20200707']
df= df[~df['date'].isin(dates)] (1st try)

 df.drop(df[df['date'].isin(dates)].index, inplace = True) (2nd try)

The data in those corresponding rows gets deleted but I'm ending up with the data frame as below

Index   date            column1       column2
0       20200705        a              1.1%
1       20200706        b              78%
2                                      0.0%
3                                      0.0%
4       20200708        a              69%

I don't know why that happens and I didn't find any source to fix this kind of issue. So I have deleted Column2 and performed the same operation and I ended up with the data frame as below

   Index   date            column1     
    0       20200705           a              
    1       20200706           b              
    2                                      
    3                                      
    4       20200708           a         

 

Note: I'm importing the data from Google sheet by using .get_all_records() method and converting that to a data frame

 df= sheet.worksheet('work_sheet').get_all_records()
    df= pd.DataFrame(df)

After the above operation, I tried sorting the data, expecting that empty rows gets deleted

 df.sort_values(by='date')

but ended up with

 **Index   date            colum1       **
       2             
       3                                    
       0       20200705        a              
       1       20200706        b              
       4       20200708        a           

I even tried by using dropna df.dropna(inplace=True) But there is no change in the result I want that empty rows to be removed please help me I tried everything, I tried to explain below please let me know incase of any questions

The strangest part is the empty lines where the data gets deleted after df= df[~df['date'].isin(dates)] operation are stored as str type and ' ' empty space as cell,

I wonder creating a dataframe out of data imported from Google sheet is causing me problems

Nikhil
  • 19
  • 3
  • 3
    Please enclose the code/output sections with "```" or using the button "{}" in the code editor – qmeeus Jul 24 '20 at 12:13
  • As mentioned by @qmeeus please follow SO rules – whatsinthename Jul 24 '20 at 12:32
  • Stupid question but are you sure that the "date" column is a string and not a number? – qmeeus Jul 24 '20 at 12:46
  • This is my first question in the Forum, I'll try to edit as per the rules here , I know it might sound stupid but yea the problem is real and yes the date is a string(i have explicitly converted to a string) – Nikhil Jul 24 '20 at 13:22
  • May be you have to convert it to date explicitly ( for filtering) , filter it out and store the results. – whatsinthename Jul 24 '20 at 13:47
  • the list I'm passing is in string format dates=[ '20200707' ]/ I have directly matched with the date type, the problem is data gets deleted but the the index for the same is not getting removed – Nikhil Jul 24 '20 at 13:59

1 Answers1

1

You can use this:

df = df[df['date'] !='20200707']
Gamopo
  • 1,600
  • 1
  • 14
  • 22