Skip to content

Instantly share code, notes, and snippets.

@clayadavis
Last active April 2, 2018 17:24
Show Gist options
  • Save clayadavis/8727278 to your computer and use it in GitHub Desktop.
Save clayadavis/8727278 to your computer and use it in GitHub Desktop.
Convert a Pandas DataFrame to a D3 data array
def df_to_d3(df):
data = []
keys, labels = df.index, df.columns
for key in keys:
values = [{'label': l, 'value': df[l][key]} for l in labels]
data.append({'values': values, 'key': key})
return data
def series_to_d3(s):
return [{'label': k, 'value': v} for k,v in s.to_dict().items()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment