Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I multiply each row by a number depending on the values of the row?

Tags:

pandas

I have this DF and I'm looking to multiply the the number of rows depending in the amount of words col3 has. Is this something that can be done in Python?

col1    col2    col3
A1      B1      a - ab - abc
A13     B13     a - ab
A27     B27     abcd

desired output

col1    col2    col3
A1      B1      a - ab - abc
A1      B1      a - ab - abc
A1      B1      a - ab - abc
A13     B13     a - ab
A13     B13     a - ab
A27     B27     abcd
like image 829
DASHIZ Avatar asked Dec 22 '25 02:12

DASHIZ


1 Answers

Use Index.repeat with Series.str.count for counting words and then repeat rows by DataFrame.loc:

df = df.loc[df.index.repeat(df['col3'].str.count('\w+'))].reset_index(drop=True)
print (df)
  col1 col2          col3
0   A1   B1  a - ab - abc
1   A1   B1  a - ab - abc
2   A1   B1  a - ab - abc
3  A13  B13        a - ab
4  A13  B13        a - ab
5  A27  B27          abcd

If there are always separated words by - is possible count it and add 1:

df = df.loc[df.index.repeat(df['col3'].str.count('-') + 1)].reset_index(drop=True)

Or solution by @sammywemmy, thank you with splitting and length of lists:

df.loc[df.index.repeat(df.col3.str.split('-').str.len())].reset_index(drop=True)
like image 109
jezrael Avatar answered Dec 25 '25 21:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!