pandas change int to str

xiaoxiao2021-02-28  2

source:

https://stackoverflow.com/questions/17950374/converting-a-column-within-pandas-dataframe-from-int-to-string

In [16]: df = DataFrame(np.arange(10).reshape(5,2),columns=list('AB')) In [17]: df Out[17]: A B 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9 In [18]: df.dtypes Out[18]: A int64 B int64 dtype: object

Convert a series

In [19]: df['A'].apply(str) Out[19]: 0 0 1 2 2 4 3 6 4 8 Name: A, dtype: object In [20]: df['A'].apply(str)[0] Out[20]: '0'

Convert the whole frame

In [21]: df.applymap(str) Out[21]: A B 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9 In [22]: df.applymap(str).iloc[0,0] Out[22]: '0'
转载请注明原文地址: https://www.6miu.com/read-2000077.html

最新回复(0)