Convert dataframe to a rec array (and objects to strings)

后端 未结 2 1072
失恋的感觉
失恋的感觉 2020-12-22 01:30

I have a pandas dataframe with a mix of datatypes (dtypes) that I wish to convert to a numpy structured array (or record array, basically the same thing in this case). For

2条回答
  •  心在旅途
    2020-12-22 02:10

    As far as I am aware, there is no native functionality for this. For example, the maximum length of all values within a series is not stored anywhere.

    However, you can implement your logic more efficiently via a list comprehension and f-strings:

    data_types = [(col, arr[col].dtype if arr[col].dtype != 'O' else \
                   f'U{df[col].astype(str).str.len().max()}') for col in arr.dtype.names]
    

提交回复
热议问题