简体   繁体   中英

Trying to plot Data from a pandas dataframe

I am trying to build a simple line-chart with a pandas dataframe. The dataframe contains a column with date entries. 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.

I've tried several approches but cannot find a solution. i think copying the data into a new dataframe would be the best solution. The Dataframe is populated from a csv by following 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:

在此处输入图像描述

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

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'
)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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