How do I convert a list/dictionary into a Dataframe?

Chris B.

I have a JSON response (sample below) that I'm trying to convert into a DataFrame. I've had several issues with the data being listed as columns (1 x 346), etc. I only need the 5 columns listed below:

area_name,
date,
month,
unemployment_rate,
year

Here's my code:

edd_ca_df = pd.DataFrame.from_dict(edd_ca, orient="index", 
                                   columns=["area_name", "month", "date", "year", "unemployment_rate"])

and here's a sample of the JSON response:

[[{'area_name': 'California',
   'area_type': 'State',
   'date': '1990-01-01T00:00:00.000',
   'employment': '14099700',
   'labor_force': '14953900',
   'month': 'January',
   'seasonally_adjusted_y_n': 'N',
   'status_preliminary_final': 'Final',
   'unemployment': '854200',
   'unemployment_rate': '5.7',
   'year': '1990'},

 {'area_name': 'California',
   'area_type': 'State',
   'date': '1990-02-01T00:00:00.000',
   'employment': '14206700',
   'labor_force': '15049400',
   'month': 'February',
   'seasonally_adjusted_y_n': 'N',
   'status_preliminary_final': 'Final',
   'unemployment': '842800',
   'unemployment_rate': '5.6',
   'year': '1990'},

Any help would be greatly appreciated.

b-fg

Since you have a list of dictionaries, this is as simple as passing all the data to a new DataFrame and specifying what columns you want to keep:

import pandas as pd

all_data = [{'area_name': 'California',
   'area_type': 'State',
   'date': '1990-01-01T00:00:00.000',
   'employment': '14099700',
   'labor_force': '14953900',
   'month': 'January',
   'seasonally_adjusted_y_n': 'N',
   'status_preliminary_final': 'Final',
   'unemployment': '854200',
   'unemployment_rate': '5.7',
   'year': '1990'},

 {'area_name': 'California',
   'area_type': 'State',
   'date': '1990-02-01T00:00:00.000',
   'employment': '14206700',
   'labor_force': '15049400',
   'month': 'February',
   'seasonally_adjusted_y_n': 'N',
   'status_preliminary_final': 'Final',
   'unemployment': '842800',
   'unemployment_rate': '5.6',
   'year': '1990'}]

keep_columns = ['area_name','date','month','unemployment_rate','year']
df = pd.DataFrame(columns=keep_columns, data=all_data)

print(df)

Output

    area_name                     date     month unemployment_rate  year
0  California  1990-01-01T00:00:00.000   January               5.7  1990
1  California  1990-02-01T00:00:00.000  February               5.6  1990

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert timestamp to datetime.date in pandas dataframe?

how do i convert a numpy array to pandas dataframe

How do I read a Parquet in R and convert it to an R DataFrame?

How do I convert an RDD with a SparseVector Column to a DataFrame with a column as Vector

How do I convert a WrappedArray column in spark dataframe to Strings?

How do I convert vincenty distance to float in pandas dataframe

How do I convert a pandas series which is multidimensional to pandas dataframe

How do i convert python list into pandas dataframe with predefined columns

How do I efficiently convert pandas dataframe to image array?

How do I convert two DataFrame columns into summed Series?

How do I convert a dataframe row into a set of pairs?

How do I convert a text table to a pandas dataframe?

R: How do I convert a dataframe of strings into POSIXt objects?

How do I convert a dataframe of strings to csv in python?

How do I convert a list of zoo objects into a dataframe

How do I convert a Python DataFrame into a NumPy array

How do I convert a numpy array into a pandas dataframe?

How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns?

How do I convert my walkscoreAPI output list to a dataframe in R?

How do I convert the results of survey::syvratio into a dataframe?

How do I convert an XML file into a dataframe/tibble in R?

How do I convert currencies to dollars in a dataframe

how do i convert from a webcomponent to pandas dataframe

How do I convert a dataframe to a dictionary with keys that are lists?

How do I convert a dataframe with time-periods into a particular format?

How do I convert a dataframe column filled with numbers to strings in python?

How do I convert this dictionary to a dataframe in python?

How do I convert a dataframe into a Dictionary of 1D

How do I convert a hierarchical dataframe to a list in R?