Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

python numpy.ndarray with shape (5,2) to dataframe with columns 'class0' and 'class1'

  1. User

    python numpy.ndarray with shape (5,2) to dataframe with columns 'class_0' and 'class_1'

  2. Assistant

    You can convert a numpy ndarray with shape (5,2) to a dataframe with columns 'class_0' and 'class_1' using the pandas library in Python. Here's an example:

    import pandas as pd
    import numpy as np
    
    # Create a numpy ndarray with shape (5,2)
    data = np.array([[1, 2],
                     [3, 4],
                     [5, 6],
                     [7, 8],
                     [9, 10]])
    
    # Convert the ndarray to a dataframe
    df = pd.DataFrame(data, columns=['class_0', 'class_1'])
    
    print(df)
    

    Output:

       class_0  class_1
    0        1        2
    1        3        4
    2        5        6
    3        7        8
    4        9       10
    

    In this example, we create a numpy ndarray called data with shape (5,2) containing some random values. We then convert this ndarray to a dataframe using the pd.DataFrame() function, specifying the column names as 'class_0' and 'class_1'. Finally, we print the resulting dataframe df.