简体   繁体   中英

Getting float values while reading Data Frame

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM