[英]How to plot bar chart(s) from csv file using python?
I have this.csv file (that I can't edit):我有这个.csv 文件(我无法编辑):
,Denmark,Norway,Sweden
TotalCases,"78,354 ","35,546 ","243,129 "
Deaths,"823","328","6,681"
Recovered,"61,461","20,956",N/A
I want to make 3 separate bar charts/graphs, one for each section (TotalCases, Deaths, Recovered).我想制作 3 个单独的条形图/图表,每个部分一个(TotalCases、Deaths、Recovered)。 However most guides I found online have the data presented the other way round, where the TotalCases are the columns instead of rows like in this scenario.
然而,我在网上找到的大多数指南都以相反的方式呈现数据,其中 TotalCases 是列而不是像这种情况下的行。 What is the right way to do this?
这样做的正确方法是什么?
Then just transpose your data frame and follow the examples!然后只需转置您的数据框并按照示例进行操作!
import pandas as pd
import numpy as np
from io import StringIO
s = StringIO("""
country,Denmark,Norway,Sweden
TotalCases,"78,354 ","35,546 ","243,129 "
Deaths,"823","328","6,681"
Recovered,"61,461","20,956",N/A
""")
df = pd.read_csv(s)
df_t = df.transpose()
df_t.columns = df_t.iloc[0, :]
df_t = df_t.iloc[1:, :]
df_t['country'] = df_t.index
Use df_t then following their examples.使用 df_t 然后按照他们的示例进行操作。
In [45]: df_t
Out[45]:
country TotalCases Deaths Recovered country
Denmark 78,354 823 61,461 Denmark
Norway 35,546 328 20,956 Norway
Sweden 243,129 6,681 NaN Sweden
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.