0

I have Sr.no columns in my csv file witch contain all integer values but will reading it as pandas data frame some integer values are converted into float why?

I have Data set contain following records

When i load it as Data Frame it shows like this

These are n th records of same data set

But this time in Data Frame SR.NO column it is showing float values

1 Answer 1

0

This is type domination.
Check this example:

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))


    A   B
0   1   2
1   3   4  <---- ALL INTEGERS

and:

df2 = pd.DataFrame([[np.nan, 6], [7, 8]], columns=list('AB'))

    A   B
0   NaN 6
1   7.0 8 <-- NOT INTEGER

You can see, 7 -> 7.0.
And more:

df.append(df2, ignore_index=True)

    A   B
0   1.0 2
1   3.0 4
2   NaN 6
3   7.0 8

Pandas automaticly define a column's type.
For change this, use pd.read_csv(..., dtype={'PUT_COL_NAME_HERE': PUT_TYPE_HERE}) or pd.astype()

Sign up to request clarification or add additional context in comments.

1 Comment

Great i got it Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.