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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With