0

This is my Dataset

Expected Dataset

This is just a Demo Dataset, I have a large dataset with millions of rows, I want to classify my shares as share bought and sold for every row, if the type is P it should be added in the column of bought and vice versa, similarly, if a row is empty it should just enter 0 in those two columns. Please suggest something without any loops as it's a pretty huge dataset. The Dataset is almost for millions of rows, so we need something proper which will be directly applicable for dataset.

Only For loop has been tried, but takes too much time.

msj here
  • 1
  • 1
  • `pivot` ID/Type/Quantity, then `join` to the original – mozway Jan 10 '23 at 08:36
  • @mozway can you please suggest a small code example here? – msj here Jan 10 '23 at 08:39
  • Can you provide a reproducible input (not an image)? – mozway Jan 10 '23 at 08:41
  • Yes, sure, I have pasted it here, please provide a code. link - https://drive.google.com/file/d/1hXBaSGNdk_0cXgBowQsIl7U92DyTBbS5/view?usp=sharing – msj here Jan 10 '23 at 08:50
  • @mozway can you please do it, I have pasted the link here, it's kind of urgent too, thank you! – msj here Jan 10 '23 at 09:03
  • I'm sorry but **urgent** is not a concern here, this is **NOT** a free coding service, rather a platform to help the community. If you're on a deadline, hire a professional. That said, what you need is `out = df.join(df.assign(Type=df['Type'].map({'P': 'Purchased', 'S': 'Sold'})).pivot(columns='Type', values='Quantity'))` – mozway Jan 10 '23 at 10:23

0 Answers0