Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert pandas pd to numpy array and back

What I want is to convert a numpy array to pandas dataframe.

df.head()
            A     B     C     D
      0    34    howdy  cow   meting
      1    23    cow    me    howdy

After tokenizing this df

df.head()
          A      B     C      D
     0    34     1     2      3
     1    23     2     4      1

converted df to numpy array for analysis with KMeans numpy array

   array [[34 ,1, 2, 3],
          [23 ,2, 4, 1]]

Question how can i convert this back to the first df i.e comparing the index of the array to the index of pandas and getting the row values

like image 951
Barbara Miranda Avatar asked Oct 31 '25 20:10

Barbara Miranda


1 Answers

I think you can use values for convert to numpy array and then DataFrame constructor:

arr = df.values
print (arr)
[[34  1  2  3]
 [23  2  4  1]]

print (pd.DataFrame(arr))
    0  1  2  3
0  34  1  2  3
1  23  2  4  1
print (pd.DataFrame(arr, index=df.index, columns=df.columns))
    A  B  C  D
0  34  1  2  3
1  23  2  4  1
like image 178
jezrael Avatar answered Nov 02 '25 10:11

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!