Repeat code for the same product group

Asked

Viewed 55 times

-2

I have a spreadsheet with thousands of products according to the example below, where the code is only in the type of service (left column), and I want it to be in all products of the same type (right column). In practice, I want code 1 to be dragged to code 2, and this to code 3, and so on. There is something ready in pandas for this?

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

1 answer

1


df.fillna('', inplace=True)

while('' in df['CODIGO'].values):

    df.loc[df['CODIGO'] == '', 'CODIGO'] = df.loc[:, 'CODIGO'].shift(1)
  • This solution will not be useful, because there are thousands of elements that are arranged in this way (one on top of the other), and I cannot look at one to know how many codes have and apply this solution. My spreadsheet was exemplifying. I want that whenever the code column shows some value, this value is repeated until you find the next one, exactly like this one in the right column. Another thing: I exemplified with code 1, 2 and 3. But it may be that the column contains code 11, then code 9, that is, it is not sequential.

  • The second part of my solution is general

  • Blz. I tried to run and give this message: /usr/lib/python3/dist-Packages/ipykernel_launcher.py:1: Futurewarning: elementwise comparison failed; returning scalar Instead, but in the Future will perform elementwise comparison """Entry point for Launching an Ipython kernel.

  • How does Dataframe look after you run it? And how is Dataframe before you run it? To help you I need to see the real Dataframe (or at least part of it); not a photo of a spreadsheet in Excel.

  • I changed the original question and inserted the dataframe into it. It is thus after executing the command that Voce suggested.

  • Thank you! And before you run?

  • Friend, your code does nothing but return that error message I gave you. Before and after executing your code, the dataframe looks exactly like this, without any changes

  • It’s more important for me to see how Dataframe is before you run. In case, if you’re saying it looks the way you showed it. Then you need to make some changes first. I will edit my reply.

  • The df before I execute the command (so I care for the pandas), is the way I posted, and then also. IE, there is no change.

  • 1

    See if you go now

  • Wonderful, it worked. And' that’s just what I need. Thank you very much.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.