Skip to content

Tag: pandas

Different aggregate function based on value of column pandas

I have the following dataframe I would like to add a new column, agg_y, which would be the the max(y) if label==”bottom” and min(y) if label==”top”. I have tried this which gives the correct result I am just looking fora one-liner solution, if possible Answer Your solution in one line …

Filling column based on conditions

In the DataFrame below I am trying to update the Value column based on the following conditions, if Action is Sell check if the Status is not – if both the conditions are true then the first two characters of the Country needs to be updated as the Value column else if Status column is – and the Ac…

How to compare differently transposed data in pandas or python

I am trying to compare or merge two different data sets and I am using pandas for that. The challenge that I am facing is that data is spread across rows in the first data set (Data1) and the other data set (Data2) has the same data spread across columns, below are the screenshots. Screenshot 1st – This…

Pandas cleaner syntax

I would like to replace the following syntax with a cleaner, chained syntax – perhaps using .pipe (similar to dplyr library in R): Sample dataset: Code to replace by piping: Expected output: Answer Here are two elegant ways to get your output: or