Attributeerror: 'Nonetype' Object has no attribute 'Insert' - Trying to insert a column

Asked

Viewed 492 times

0

Good morning ! I’m new in python and I’m trying to build a script that deletes some lines and includes a column in a dataframe only that for some reason I can’t understand it returns this error.

import pandas as pd

df = pd.read_excel('planilha.xlsx, sheet_name=1)

df = df.drop(['ID EMPRESA', 'NF 2', 'VALOR NF 2', 'VAT'], axis=1)

df = df.drop(df[df.TPV == 0.00].index, inplace=True)

df = df.insert(loc=25, column='Crédito BOAC', value=None)

df.head()


removido por seg.

Como eu deveria fazer neste caso?
  • put in doubt an example of your dataset, but this error is when the object type is None. As your dataset to test where the error occurred and why.

  • Imonferrari, I edited it

  • The error happens in the call of to_excel, but in your code it is not even used. Are you sure you have put the correct code or it completely?

  • By error shows that you are calling a function from an object None, probably in the middle of the way something happened to this object that he was like None.

  • Woss I had put # in insert to see if the rest went, so the mistake was in the to_excel. Fixed, but error persists rs.

  • Imonferrari, so I understood this too, but I don’t know how to fix it or at what point it happened.

  • enter all the code and make the data available (you can remove the confidential data if you have it). Anything uploads the data file to some cloud service and posts the link here.

  • In the second drop, you defined inplace=True. This will make the drop occurs directly on own df, returning None. If you want to get the return, remove the parameter inplace.

  • It’s not working. I removed the inplace=True but the error persists.

  • Imonferrar, follow the links: script: https://drive.google.com/file/d/1W0OZFMoBOKbS2IxiB3UtySmCMBgJcAlw/view?usp=sharing excel: https://drive.google.com/file/1qV1lxJ0idIo6iI6_FqQayYeUe5Vf9Wmu/view?usp=sharing

Show 5 more comments

1 answer

0

You must change it:

df = df.drop(df[df.TPV == 0.00].index, inplace=True)

df = df.insert(loc=25, column='Crédito BOAC', value=None)

For that reason:

df = df.drop(df[df.TPV == 0.00].index)

df.insert(loc = 21, column = 'Crédito BOAC', value = None)

Insert already changes its data frame without having to make a copy inside itself.

If you want to use inplace = True:

df.drop(df[df.TPV == 0.00].index, inplace = True)

df.insert(loc = 21, column = 'Crédito BOAC', value = None)
  • It worked ! You’re awesome ! Thanks man.

  • For nothing! Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.