Skip to content
Advertisement

Tag: pandas

Why do I have more row after the left merge and drop_duplciates()?

iso_selected.shape gives the result: (257, 2) gravity.shapegives the result: (4428288, 79) and I merge them in the following way: gravity1 = gravity.merge(iso_selected, how=”left”, left_on = “iso3_o”, right_on=”iso3″).drop_duplicates() gravity1.shape gives the result: (4571136, 81) Why would I have more rows than 4428288 ? Answer You need remove duplicates before DataFrame.merge by column iso3_o:

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 solution is: Solution without groupby, thank you @ouroboros1: Another idea

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 Action column

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 is Data1

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

Advertisement