How do I convert two DataFrame columns into summed Series?

Chuck

I have a pandas DataFrame that looks like this:

              date  sku  qty
0       2015-10-30  ABC    1
1       2015-10-30  DEF    1
2       2015-10-30  ABC    2
3       2015-10-31  DEF    1
4       2015-10-31  ABC    1
...            ...  ...  ...

How can extract all of the data for a particular sku and sum up the qty by date. For example, the ABC SKU?

2015-10-30   3
2015-10-31   1
       ... ...

The closest I've gotten is a hierarchal grouping with sales.groupby(['date', 'sku']).sum().

Quang Hoang

If you will work with all (or several) sku, then:

agg_df = df.groupby(['sku','date']).qty.sum()

# extract some sku data
agg_df.loc['ABC']

Output:

date
2015-10-30    3
2015-10-31    1
Name: qty, dtype: int64

If you only care for ABC particularly, then it's better to filter it first

df[df['sku'].eq('ABC')].groupby('date')['qty'].sum()

The output would be the same as above.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert Series index into two columns as a DataFrame

How do I convert a series of mixed text and number values to a single string with consecutive number values summed?

How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns?

Convert a dataframe with two columns to a Panda Series

How do I aggregate data into multiple different summed columns by criteria?

How do I convert a pandas series which is multidimensional to pandas dataframe

How to convert a particular dataframe into a series by combining columns

How do i convert python list into pandas dataframe with predefined columns

How can I avoid seeing "sum()" at the top of a query where I've summed two columns?

How do I convert pandas.core.series.Series back to a Dataframe following a groupby?

How can I convert a Group By series to a Dataframe?

How do I convert a series of repeated data rows into columns of multiple records using Python?

How do I convert multiple `string` columns in my dataframe to datetime columns?

How do I sum up when complying to two conditions and then put the summed data in a new data frame?

How to convert multiple dictionary keys in a Pandas Series to columns in a DataFrame?

How do I add two new columns on the basis of the values of multiple other columns in a pandas dataframe?

How do I compare two columns in a Pandas DataFrame and output values from other columns based on the match?

multiple series appended in a list using df.iterrows. How do I convert it into a dataframe?

How to convert Dataframe into Series?

How do I turn a dataframe into a series of lists?

How Do I Create New Column In Pandas Dataframe Using Two Columns Simultaneously From A Different Dataframe?

R question: I modified two categorical columns, how do I embedded back into the original dataframe?

How do i unstack a dataframe so i can divide two columns?

How do I count the number of rows I have summed values in 1 column based on matching values in 3 other columns [R]?

How do I convert json into a dataframe making a default row into a new columns based on delimiter?

Convert lst with two columns to dataframe?

How to convert one row two columns dataframe into multiple rows two columns dataframe

How do I convert columns to rows

How do I calculate the Levenshtein distance between two Pandas DataFrame columns?