arrayspandasdataframecsv

How to convert a datafram into two arrays : One containing the data and the second containing column names


I have a .csv file that I want to convert into numpy arrays. I want one numpy array containing the header's names, and a second numpy array containing the data. I managed to read my file and put it in a dataframe format. But how can i convert this dataframe into two arrays please ?

Here is what I wrote to convert it into a dataframe and please find on this link my .csv file text

import pandas as pd

from pathlib import Path



universal_path = Path(r"file\file\data.csv")

data = pd.read_csv(universal_path , sep = ';'  , skiprows = 0,  
                   encoding = 'ISO-8859-1', engine = 'python', header = 0 ) 

Solution

  • To convert your header and data in numpy arrays you can do the following:

    df = pd.DataFrame({
        'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]
    })
    
    # Extract headers as a NumPy array
    headers = df.columns.to_numpy()
    
    # Extract data as a NumPy array
    data = df.to_numpy()
    
    print("Headers:", headers)
    Headers: ['A' 'B' 'C']
    
    print("Data:\n", data)
    Data:
     [[1 4 7]
     [2 5 8]
     [3 6 9]]