How do I convert my 2D numpy array to a pandas dataframe with given categories?

Arran

I have an array called 'values' which features 2 columns of mean reaction time data from 10 individuals. The first column refers to data collected for a single individual in condition A, the second for that same individual in condition B:

array([[451.75      , 488.55555556],
   [552.44444444, 590.40740741],
   [629.875     , 637.62962963],
   [454.66666667, 421.88888889],
   [637.16666667, 539.94444444],
   [538.83333333, 516.33333333],
   [463.83333333, 448.83333333],
   [429.2962963 , 497.16666667],
   [524.66666667, 458.83333333]])

I would like to plot these data using seaborn, to display the mean values and connected single values for each individual across the two conditions. What is the simplest way to convert the array 'values' into a 3 column DataFrame, whereby one column features all the values, another features a label distinguishing that value as condition A or condition B, and a final column which provides a number for each individual (i.e., 1-10)? For example, as follows:

Value    Condition    Individual
451.75   A            1
488.56   B            1
488.55   A            2

...etc

yatu

melt

You can do that using pd.melt:

pd.DataFrame(data, columns=['A','B']).reset_index().melt(id_vars = 'index')\
    .rename(columns={'index':'Individual'})

 Individual variable       value
0            0        A  451.750000
1            1        A  552.444444
2            2        A  629.875000
3            3        A  454.666667
4            4        A  637.166667
5            5        A  538.833333
6            6        A  463.833333
7            7        A  429.296296
8            8        A  524.666667
9            0        B  488.555556
10           1        B  590.407407
11           2        B  637.629630
12           3        B  421.888889
13           4        B  539.944444
14           5        B  516.333333
15           6        B  448.833333
16           7        B  497.166667
17           8        B  458.833333

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how do you convert a dataframe into 2d numpy array

How do I convert a numpy array into a pandas dataframe?

how do i convert a numpy array to pandas dataframe

How do I map a numpy array and an indices array to a pandas dataframe?

How to transform a sparse pandas dataframe to a 2d numpy array

How do I convert a Python DataFrame into a NumPy array

How do I open a binary matrix and convert it into a 2D array or a dataframe?

How do I convert a pandas Series or index to a Numpy array?

Pandas to_numpy() results in an array of lists. How do I get a 2D numpy array from this?

How to convert 3-D Numpy array to Pandas Dataframe?

How do I efficiently convert pandas dataframe to image array?

How to convert a pandas dataframe to NumPy array

How to copy numpy 2d array in pandas 2d dataframe

Convert a Pandas DataFrame to a NumPy 2d Array based on 2 columns as dimensions and 1 as value

How do i convert a Set into 2D array in java?

How do I convert a String to an 2D Array of ints?

How to convert a pandas dataframe column to an image array i.e. a numpy array with shape (n,n) in Python?

How to convert my array obtained from C++ to a 2D numpy array in Python without for loop

Convert a Numpy array into a Pandas DataFrame

Convert numpy array to pandas dataframe

How can i convert array([array([]) to 2d numpy array?

When i convert my numpy array to Dataframe it update values to Nan

Convert 2D numpy.ndarray to pandas.DataFrame

How to convert 3d np array to 2d for pandas dataframe

How to convert each row in Pandas DF to 2D numpy array?

pandas/numpy: I have an array with a dictionary inside. How do I create a DataFrame from this?

how to get a 2d numpy array from a pandas dataframe? - wrong shape

Python : Convert 2D numpy array to pandas series

(Python Numpy) How can I create new 3D array from given 2D array?