1
votes
import pandas as pd   
import csv 
import numpy as np

readfile = pd.read_csv('51.csv')
filevalues= readfile.loc[readfile['Customer'].str.contains('Lam Dep', na=False), 'Jul-18\nQty']
filevalues.replace(" ", "")
filevalues = filevalues.replace(r'^\s*$', np.nan, regex=True) 
filevalues.dropna()
int_series = filevalues.astype(int)
calculated_series = int_series.apply(lambda x: x*(1/1.2))

print(calculated_series)

In my csv files, there are cells with NaNs and empty strings ( or perhaps white spaces) I attempt to get rid of cells with white spaces and drop the NaN values, however I run into the error:

ValueError: cannot convert float NaN to integer

The other methods I have found on stackoverflow have proven to not work for this code. Any idea of how I can move forward? This is especially confusing because my code specifies to drop NaNs and then the error states: cannot convert float NaN to integer

1
You probably need filevalues.dropna(inplace=True) or filevalues = filevalues.dropna() since, dropna() does not remove in original dataframe when you don't specify inplace=True instead it returns dataframe with with NA entries dropped. - student
@student I just ran that and gave me the same error. - Michael Norman
Can you try using filevalues.dropna(how='any',inplace=True)? - student
@student Tried it, and sadly gave the same error. - Michael Norman
Can you look into the output for filevalues[filevalues.isnull()]? - student

1 Answers

1
votes

You can try as following:

filevalues = filevalues.replace(" ", "", regex=True)
filevalues.replace("", np.nan, inplace=True) # replace empty string with np.nan
filevalues.dropna(inplace=True) # drop nan values
int_series = filevalues.astype(int) # change type

calculated_series = int_series.apply(lambda x: x*(1/1.2))

print(calculated_series)