-1

Sample of Dict: {'7/24/20': 'Red Bank: 280', '7/23/20': 'Red Bank: 279', '7/22/20': 'Red Bank: 277'} I can't get it imported into pandas as date, town, count.

df = pd.DataFrame.from_dict(thelist, orient='index')
red_bank_covid_cnt = df[0].str.split(':', expand=True)
red_bank_covid_cnt.reindex(columns=['n']+red_bank_covid_cnt.columns[:-1].tolist())
red_bank_covid_cnt.info()
red_bank_covid_cnt.head()

output: 
Index: 118 entries, 7/24/20 to 3/21/20
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   0       118 non-null    object
 1   1       118 non-null    object
dtypes: object(2)
memory usage: 2.8+ KB
0   1
7/24/20 Red Bank    280
7/23/20 Red Bank    279
7/22/20 Red Bank    277
7/21/20 Red Bank    275
7/17/20 Red Bank    269

I am trying to make a line graph. 
orphyux
  • 41
  • 6

1 Answers1

0

One more way of doing it is to modify the dictionary into a list of dictionaries

In [36]: a
Out[36]:
{'7/24/20': 'Red Bank: 280',
 '7/23/20': 'Red Bank: 279',
 '7/22/20': 'Red Bank: 277'}

In [37]: modified_list = [{"Date":k,"Bank":v.split(":")[0],"Number":v.split(":")[1]} for k,v in a.items()]

In [38]: df = pd.DataFrame(modified_list)

In [40]: df["Date"] = pd.to_datetime(df["Date"])

In [41]: df
Out[41]:
        Date      Bank Number
0 2020-07-24  Red Bank    280
1 2020-07-23  Red Bank    279
2 2020-07-22  Red Bank    277

To plot line graph

df["Number"] = df["Number"].astype(int)
df = df.set_index("Date")
df.plot.line(y="Number")
bigbounty
  • 16,526
  • 5
  • 37
  • 65