-3

I have a dictionary

x={'XYZ': [4, 5, 6], 'ABC': [1, 2, 3]}

I want a pd.DataFrame like this:

     'SomeColumnName'
'XYZ'   [4,5,6]
'ABC'   [1,2,3]

Whatever I do, it splits the list of x.values() in 3 separate columns. I could do a '~'.join before creating the Dataframe. Just wondering if there was an easier way

Gene Burinsky
  • 9,478
  • 2
  • 21
  • 28
Pankaj Singh
  • 526
  • 7
  • 21

2 Answers2

1

Why don't you just input the data as:

x={'XYZ': [[4, 5, 6]], 'ABC': [[1, 2, 3]]}

Then you get:

In [7]: pd.DataFrame(x).transpose()
Out[7]:
             0
ABC  [1, 2, 3]
XYZ  [4, 5, 6]

You can recode your dictionary using:

for key in x.keys():
        x[key] = [x[key]]
Gene Burinsky
  • 9,478
  • 2
  • 21
  • 28
  • AH, beautiful... that is a better way. – Pankaj Singh Dec 01 '16 at 21:30
  • @PankajSingh, check out this introduction to `Pandas` [data structures](http://pandas.pydata.org/pandas-docs/stable/dsintro.html). I think the reason you're being downvoted (though the voters should certainly say why they're downvoting), is because the question pertains to afundamental element of `Pandas` – Gene Burinsky Dec 02 '16 at 04:08
0

Ok, this is how I did it

z = pd.DataFrame.from_records(list(x.items()),columns=['A','SomeColumnName'],index='A')

Problem was - I wasnt using list() for data

Pankaj Singh
  • 526
  • 7
  • 21