-3

I have a Data Frame with 15 columns suppose out of which i want only 6. I am performing aggregate and then group by but it is throwing error.

def my_compute_function(my_input):

    df=pd.DataFrame(my_input)
    df2=df[(df['D'] == "Validated")]
    df2[['A','E','F']]=df2[['A','E','F']].apply(pd.to_numeric) 
    df3=df2[['A','B','C','D','E','F']].groupby(['B','C','D']).agg({'A': 
   'max','E': 'max','F': 'max'}).reset_index()

return df3    

So i want only 6 columns A,B,C,D,E,F.
When i am adding this line

df2[['A','E','F']]=df2[['A','E','F']].apply(pd.to_numeric)

it is throwing error that

ValueError: can not infer schema from empty dataset.

Mouse on the Keys
  • 322
  • 1
  • 5
  • 13
vatsal
  • 63
  • 10

2 Answers2

3

This should work.

df[['A', 'B', 'C']] = df[['A', 'B', 'C']].apply(pd.to_numeric)  

This will break

# Ignore this comment, I needed SO to show a blank
# line followed by code indented by a space

 df[['A', 'B', 'C']] = df[['A', 'B', 'C']].apply(pd.to_numeric)  
piRSquared
  • 285,575
  • 57
  • 475
  • 624
1

I think you can copy whitespaces from working code before problematic line:

#some code before, copy whitespaces from starts of line to first letter
    df = pd.read_csv(file)
^^^^copy here

    df[['A','B','C']]=df[['A','B','C']].apply(pd.to_numeric)  
^^^^paste here
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • still same error IndentationError: Unexpected Indent. – vatsal Aug 16 '17 at 07:50
  • when i am assigning this Data Frame to another Data Frame then my code is executing fine but then it is giving error - cannot infer to an empty dataset. – vatsal Aug 16 '17 at 08:25
  • Is possible see your code? And if possible, the best is send me your file to my email (in my profile) for check this problematic line. Without it is hard find problem connected with indent error. thanks. – jezrael Aug 16 '17 at 08:27