Search code examples
pythonarraysnumpyconcatenationpca

ValueError: zero-dimensional arrays cannot be concatenated,


I have two arrays with axis=0 (there are the result of the mean and the std of a df):

df_cats = 
0      58.609619
1     105.926514
2      76.706543
3      75.405762
4      68.937744
         ...    
75    113.124268
76    125.557373
77    130.514893
78    141.373779
79    109.185791
Length: 80, dtype: float64 0     63.540835
1     55.053429
2     96.221076
3     42.963771
4     57.447924
        ...    
75    42.080755
76    55.309517
77    38.997856
78    57.364695
79    40.197461
Length: 80, dtype: float64

df_dogs = 
0      86.870361
1     153.085205
2      89.576416
3     139.721924
4     107.218750
         ...    
75    129.498291
76    108.676025
77    113.125732
78    145.829346
79    100.272461
Length: 80, dtype: float64 0     57.699218
1     71.814790
2     40.130439
3     44.966932
4     48.964512
        ...    
75    50.994298
76    58.257198
77    89.240987
78    58.945353
79    68.841721
Length: 80, dtype: float64

And I'm trying to concatenate the two arrays with axis=1, using this code:

dogs_and_cats = np.concatenate((df_dogs, df_cats), axis=1)

but always have this problem:

**ValueError:** zero-dimensional arrays cannot be concatenated

How can i can concatenate that?


Solution

  • One dimensionnal arrays don't have a second dimension. This is where your problem comes from. You can concatenate by modifying the shape of these arrays to turn them into 2D arrays with only one column with the following code :

    df_dogs.shape=(df_dogs.shape[0],1)
    df_cats.shape=(df_cats.shape[0],1)
    

    And now, you can concatenate your arrays.