简体   繁体   English

如何使用 python 从 csv 文件中获取 plot 条形图?

[英]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.

相关问题 使用pandas和Matplotlib中的csv数据在python中绘制条形图 - Plot bar chart in python using csv data in pandas & Matplotlib 想要 plot 中文本文件中数据的条形图 Python - Wanting to plot a bar chart from data in a text file in Python 如何从csv文件中提取特定列并使用python进行绘图 - how to extract speciific columns from a csv file and plot using python 如何使用python从csv文件创建饼图 - how to create a pie chart from csv file using python 如何使用输入 *.txt 文件绘制一个非常简单的条形图(Python、Matplotlib)? - How to plot a very simple bar chart (Python, Matplotlib) using input *.txt file? 如何使用pandas python绘制堆积条形图 - How to plot a stacked bar chart using pandas python 如何在python中使用matplotlib绘制叠加条形图? - How to plot a superimposed bar chart using matplotlib in python? 如何使用 python 中 matplotlib 中的相同 plt plot 并保存饼图和条形图 - How to plot and save pie chart and bar graph using same plt from matplotlib in python Python - 当数据不是数字时,如何从 csv 文件制作条形图 - Python - How to make a bar chart from csv file when the data is not numeric 使用 matplotlib 和 CSV 文件中的数据制作条形图 - Making a bar chart by using matplotlib with data from CSV file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM