2

I have the following pandas Series (which is actually the output of running value_counts() on a certain column - "Tipo de vivienda" - of a DataFrame:

enter image description here

With visualizaion libraries such as Seaborn, Bokeh etc. I can form a bar chart directly on this Series, without additional wrangling or conversion. Yet for d3py, bar charts can only be generated from DataFrames (I believe). So I convert the series to a DataFrame:

enter image description here

I then set about plotting the bar chart:

p = d3py.PandasFigure(v_as_df)

At this point, I need to tell d3py that I want a Bar chart. I also need to tell it the titles of the columns, and this is where things fall down. Calling v_as_df.columns() gives me:

Index([u'Tipo de vivienda'], dtype='object')

Somewhat guessing what the columns names are, I try:

p += d3py.Bar('', 'Tipo de vivienda') # x, y

... but nothing gets displayed when I call p.show(), and I am guessing it's because I don't have a true DataFrame, but rather a Series casted as such.

Is calling DataFrame() on the Series the wrong way to go?

  • Python: 2.7.11
  • pandas: 0.18.0
Pyderman
  • 14,809
  • 13
  • 61
  • 106

1 Answers1

1

I don't think casting the Series as a DataFrame creates any problem.

However this looks wrong

p += d3py.Bar('', 'Tipo de vivienda') # x, y

The empty string is probably the source of the error.

Your DataFrame is composed of one index ('casa'), and one column ('Tipo de vivienda')

You're passing for you x argument an empty string, so d3py looks for a column that name is '',doesnt find it, and fails.

Solutions : 1. Check the doc of d3py and find how to pass an index as a value for the chart 2. Do the following :

v_as_df = v_as_df.reset_index()  # Transform your index into a column
print v_as_df.columns
### WILL output something like :**['index', 'Tipo de vivienda']**

then :
p = d3py.PandasFigure(v_as_df)
# adapt *'index'* with whatever you get from printing v_as_df.columns above
p += d3py.Bar('index', 'Tipo de vivienda') 
knightofni
  • 1,906
  • 3
  • 17
  • 22
  • Your suggested approach doesn't actually result in the plot being shown, but I can see your logic. Thanks. – Pyderman Apr 04 '16 at 21:14