Python - Turn all items in a Dataframe to strings

后端 未结 4 1057
梦毁少年i
梦毁少年i 2020-12-15 16:18

I followed the following procedure: In Python, how do I convert all of the items in a list to floats? because each column of my Dataframe is list, but instead o

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 17:00

    You can use this:

    df = df.astype(str)
    

    out of curiosity I decided to see if there is any difference in efficiency between the accepted solution and mine.

    The results are below:

    example df:

    df = pd.DataFrame([list(range(1000))], index=[0])
    

    test df.astype:

    %timeit df.astype(str) 
    >> 100 loops, best of 3: 2.18 ms per loop
    

    test df.applymap:

    %timeit df.applymap(str)
    1 loops, best of 3: 245 ms per loop
    

    It seems df.astype is quite a lot faster :)

提交回复
热议问题