Adarsh Mishra
Adarsh Mishra

Reputation: 7

Converting a python object to pandas dataframe

I am trying to convert this python object to a pandas dataframe. But it's saying that it's not 1-Dimensional so it cannot convert. It was originally a .mat file that I converted to a python list. The list looks like this when printed:

{'__header__': b'MATLAB 5.0', '__version__': '1.0', '__globals__': [], 'val': array([[-20, -17,  -2, ..., -11,  -4, -11],
[-12,  -8,  -5, ...,  -9,  -9,  -9]], dtype=int16)}

Upvotes: 0

Views: 25269

Answers (2)

Shubham Shaswat
Shubham Shaswat

Reputation: 1310

Try this way:

if you want to 2 columns

pd.DataFrame(data['val'].T)

Output:

0   1
0   -20 -12
1   -17 -8
2   -2  -5
3   -11 -9
4   -4  -9
5   -11 -9

else you can do

pd.DataFrame(data['val'])

Output :

0   1   2   3   4   5
0   -20 -17 -2  -11 -4  -11
1   -12 -8  -5  -9  -9  -9

Upvotes: 2

Salman Hammad
Salman Hammad

Reputation: 41

I think you are looking for this function:

[https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html]

Upvotes: 0

Related Questions