简体   繁体   English

尝试从 pandas dataframe 获取 plot 数据

[英]Trying to plot Data from a pandas dataframe

I am trying to build a simple line-chart with a pandas dataframe.我正在尝试使用 pandas dataframe 构建一个简单的折线图。 The dataframe contains a column with date entries. dataframe 包含一个包含日期条目的列。 For each date there are several rows of data.每个日期都有几行数据。 I want to group the data by the date, count the entries per group and have a altair linechart present the data.我想按日期对数据进行分组,计算每组的条目并让 altair 折线图显示数据。

I've tried several approches but cannot find a solution.我尝试了几种方法,但找不到解决方案。 i think copying the data into a new dataframe would be the best solution.我认为将数据复制到新的 dataframe 将是最好的解决方案。 The Dataframe is populated from a csv by following function Dataframe 由 csv 填充,遵循 function

def read_csv(rows):
    parse_dates = ['Gemeldet_Am']
    data = pd.read_csv(path+file,nrows=rows, parse_dates=parse_dates)
    data['Gemeldet_Am'] = pd.to_datetime(data['Gemeldet_Am']).dt.date
    return data

A print of the dataframe looks like this: dataframe 的打印如下所示:

在此处输入图像描述

I want to group by the everything insinde the blue box and then count the data in the red box.我想按蓝色框中的所有内容进行分组,然后计算红色框中的数据。 The altair chart should then show for each day(blue data) the sum of the red data然后,altair 图表应显示每天(蓝色数据)红色数据的总和

Here is an example of a line chart with counts grouped by date with data that is similar to yours:以下是一个折线图示例,其中包含按日期分组的计数,其数据与您的数据相似:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'date': pd.to_datetime(['2021-01-13', '2021-01-13', '2021-01-13', '2021-01-13',
                            '2021-01-14', '2021-01-14', '2021-01-14', '2021-01-15',
                            '2021-01-15', '2021-01-16', '2021-01-17', '2021-01-17']),
    'SAG': ['SAG-2101-1350', 'SAG-2101-1352', 'SAG-2101-1355', 'SAG-2101-1370',
            'SAG-2101-1373', 'SAG-2101-1378', 'SAG-2101-1382', 'SAG-2101-1385', 
            'SAG-2101-1391', 'SAG-2101-1393', 'SAG-2101-1394', 'SAG-2101-1397']
})

alt.Chart(df).mark_line().encode(
    x='date:T',
    y='count():Q'
)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM