-2

I have a dataframe with a lot of columns and rows (> 10000), but I need to create a column based on the minimum value from another one

Example dataframe:

enter image description here

So, I need the column "Cantidad min" to contain the minimum value for that group of "CodProducto":

Example:

enter image description here

How can I do in python?

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 2
    [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) Try to create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Vladimir Fokow Aug 23 '22 at 16:23
  • Is this a [tag:pandas] dataframe? Please provide your data in a way we can actually use it (see [How to make good reproducible pandas examples](//stackoverflow.com/q/20109391/843953)). – Pranav Hosangadi Aug 23 '22 at 16:23
  • 1
    Your question is a duplicate of this one: https://stackoverflow.com/questions/51074911/pandas-get-minimum-of-one-column-in-group-when-groupby-another – Pranav Hosangadi Aug 23 '22 at 16:24

1 Answers1

3

Please try follow one.

dataframe["Cantidad min"] = df.groupby(dataframe)["CodProducto"].min()

I can't test this code on your data because you didn't share yours. If it doesn't works, please provide me your data as .csv format.

levintech
  • 46
  • 5