Re-initialise the dataframe did not work in pandas

Question:

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))

enter image description here

Asked By: Sriram

||

Answers:

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)
Answered By: Gunesh Shanbhag
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.