Dump a NumPy array into a csv file

前端 未结 10 1132

Is there a way to dump a NumPy array into a CSV file? I have a 2D NumPy array and need to dump it in human-readable format.

10条回答
  •  死守一世寂寞
    2020-11-22 13:18

    I believe you can also accomplish this quite simply as follows:

    1. Convert Numpy array into a Pandas dataframe
    2. Save as CSV

    e.g. #1:

        # Libraries to import
        import pandas as pd
        import nump as np
    
        #N x N numpy array (dimensions dont matter)
        corr_mat    #your numpy array
        my_df = pd.DataFrame(corr_mat)  #converting it to a pandas dataframe
    

    e.g. #2:

        #save as csv 
        my_df.to_csv('foo.csv', index=False)   # "foo" is the name you want to give
                                               # to csv file. Make sure to add ".csv"
                                               # after whatever name like in the code
    

提交回复
热议问题