Skip to content
Advertisement

dataframe operations – column attributes to new columns in a new subset dataframe with conditions

I have the dataframe df1 with the columns type, Date and amount. My goal is to create a Dataframe df2 with a subset of dates from df1, in which each type has a column with the amounts of the type as values for the respective date.

Input Dataframe:
df1 =

JavaScript

Desired Output, if the subset of Dates are 2017-02-02 and 2017-02-04:
df2 =

JavaScript

I tried it like this:

JavaScript

but with this solution I get a lot of NaNs, it seems my comparison condition is wrong. This is the Ouput I get:

JavaScript

Advertisement

Answer

You can use .pivot_table() and then filter data:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement