Reputation: 3047
I have the following numpy matrix:
array([[64, 22,],
[58, 64],
[42, 31])
And i want to get the following:
pd.DataFrame({'one':"64 22", 'two':"42 31"})
My purpose is to convert each row in the numpy.array to a string that will be used for a pandas dataframe. Is there some built in pandas function to the rescue?
Upvotes: 5
Views: 20022
Reputation: 862511
IIUC you can use DataFrame
constructor and apply
join
:
import pandas as pd
import numpy as np
arr = np.array([[64, 22,], [58, 64], [42, 31]])
print arr
[[64 22]
[58 64]
[42 31]]
li = ['one','two','three']
df = pd.DataFrame(arr, dtype='str', index=li)
print df
0 1
one 64 22
two 58 64
three 42 31
print df.apply(lambda x: ' '.join(x), axis=1)
one 64 22
two 58 64
three 42 31
dtype: object
Upvotes: 5