how to round/remove traling “.0” zeros in pandas column?

后端 未结 11 1274
轮回少年
轮回少年 2020-12-06 10:15

I\'m trying to see if I can remove the trailing zeros from this phone number column.

Example:

0
1      8.00735e+09
2      4.35789e+09
3      6.10644e         


        
11条回答
  •  南方客
    南方客 (楼主)
    2020-12-06 10:29

    In Pandas/NumPy, integers are not allowed to take NaN values, and arrays/series (including dataframe columns) are homogeneous in their datatype --- so having a column of integers where some entries are None/np.nan is downright impossible.

    EDIT:data.phone.astype('object') should do the trick; in this case, Pandas treats your column as a series of generic Python objects, rather than a specific datatype (e.g. str/float/int), at the cost of performance if you intend to run any heavy computations with this data (probably not in your case).

    Assuming you want to keep those NaN entries, your approach of converting to strings is a valid possibility:

    data.phone.astype(str).str.split('.', expand = True)[0]

    should give you what you're looking for (there are alternative string methods you can use, such as .replace or .extract, but .split seems the most straightforward in this case).

    Alternatively, if you are only interested in the display of floats (unlikely I'd suppose), you can do pd.set_option('display.float_format','{:.0f}'.format), which doesn't actually affect your data.

提交回复
热议问题