python: converting an numpy array data type from int64 to int

前端 未结 4 1273
甜味超标
甜味超标 2021-01-03 23:59

I am somewhat new to python and I am using python modules in another program (ABAQUS). The question, however, is completely python related.

In the program, I need to

相关标签:
4条回答
  • 2021-01-04 00:25

    numpy.ndarray.tolist will do it:

    a.tolist()
    

    If your data is a pandas series you can call their tolist wrapper with the same result.

    0 讨论(0)
  • 2021-01-04 00:33

    @J.F. Sebastian's answer:

    a.astype(numpy.int32)
    
    0 讨论(0)
  • 2021-01-04 00:33

    Use the item() method for numpy.int64 object, as Mike T's answer in another similar question explained.

    Official documentation is here: https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.chararray.item.html#numpy.chararray.item

    0 讨论(0)
  • 2021-01-04 00:40

    If it's a pandas serise, you can first convert it to Dataframe, then use df.to_dict(), then the numpy.int64 will convert to int

    In [1]: import pandas as pd
    
    In [2]: import numpy as np
    
    In [3]: df = pd.DataFrame(np.random.randint(5,size=(3,4)),
    index=np.arange(3))
    
    In [4]: type(df[0][0])
    
    Out[4]: numpy.int64
    
    In [5]: dict_of_df = df.to_dict()
    
    In [6]: type(dict_of_df[0][0])
    
    Out[6]: int
    
    0 讨论(0)
提交回复
热议问题