I have a big dataframe (Data) which some of its value is ''.n''. I tried to change it to NaN then remove it but it did not work:
`Data1 = Data.replace('.n','NaN')
`Data1=Data1.astype(float)
then I received this error: ValueError: could not convert string to float: 'Cholet Dupont'
so I checked data type:
Data.dtypes
NAME object COUNTRY object year int64 DATA5260 object DATA9525 object DATA9960 object DATA8634 object DATA6190 object DATA9460 object DATA6030 object DATA6040 object
as you can see because most of the columns include ''.n'' , it shows object! so how can I remove ''.n'' from all the rows and change my data to float because they are not int(I think as it includes like 11.2 or ...)
any idea ?
CodePudding user response:
Let us say you'd like to convert the column DATA5260,DATA9525
to numeric. A pretty simple way to do it using pandas
is :-
columns_to_convert=['DATA5260','DATA9525']
for cols in columns_to_convert:
df[cols] = pd.to_numeric(df[cols], errors='coerce')