'Combine values in dataframe on multiple rows on date and name [duplicate]
I have a dataframe dataset where I would like to combine some of the data. At the moment I've tried by using .groupby() and .sum() but the result I get is not what I expected.
My data set atm looks like the following:
| date | value | name |
|---|---|---|
| 2022-01-19 | 27 | podcast_processed |
| 2022-01-19 | 25 | podcast_processed |
| 2022-01-19 | 23 | podcast_processed |
| 2022-01-19 | 23 | podcast_processed |
| 2022-01-19 | 23 | n_known_errors |
| 2022-01-19 | 23 | n_known_errors |
| 2022-01-19 | 23 | n_known_errors |
| 2022-01-19 | 23 | n_known_errors |
| 2022-01-19 | 0 | n_unknown_errors |
| 2022-01-19 | 0 | n_unknown_errors |
| 2022-01-19 | 2 | n_unknown_errors |
| 2022-01-19 | 0 | n_unknown_errors |
What I would like to achieve is to combine the values on the different names. How can this be done?
So the data would be (In propper dataframe format):
| date | value | name |
|---|---|---|
| 2022-01-19 | 98 | podcast_processed |
| 2022-01-19 | 69 | n_known_errors |
| 2022-01-19 | 2 | n_unknown_errors |
My code:
from xxx.input import get_automation_metrics
def graph_visual_data(script_title):
data = get_automation_metrics(script_title)
data = data[data.name.isin(['podcast_processed', 'n_unknown_errors', 'n_known_errors'])][['date','value','name']]
data.date = data.date.dt.date
data.value = data.value.astype(int)
data = data.groupby(['name','date'])['value'].sum()
fig = alt.Chart(data).mark_bar(size=10).encode(
x='date',
y='value',
color='name',
tooltip=['name', 'value']
).interactive()
st.altair_chart(fig, use_container_width=True)
As an extra if possible, I need to subtract the combined values from n_unknown_errors and n_known_errors from the total of podcast_processed. So the data would look like (In propper dataframe format):
| date | value | name |
|---|---|---|
| 2022-01-19 | 27 | podcast_processed |
| 2022-01-19 | 69 | n_known_errors |
| 2022-01-19 | 2 | n_unknown_errors |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
