I am new to pandas and I am trying to re-initialise the dataframe using the pd.DataFrame constructor with dtype=object. This is because the column from excel is reading it as float instead of integer. When I try to change it to object, the data seems the same. It still showing as float. The datatype have changed to object but the data did not change to the original number. Below is the code and screenshot of the output:
df = pd.read_csv (filename , low_memory=False)
df = pd.DataFrame(df)
print(df['C9_O1'].head(165))
df = pd.DataFrame(df, dtype=object)
print(df['C9_O1'].head(165))
CodePudding user response:
Try this: This replaces nan/ missing value with 0 and float values with int.
df = pd.read_csv (filename , low_memory=False)
df = pd.DataFrame(df)
print(df['C9_O1'].head(165))
df = pd.DataFrame(df, dtype=object)
print(df['C9_O1'].head(165))
df["C9_O1"] = df["C9_O1"].fillna(0).astype(int)