DASHIZ
DASHIZ

Reputation: 33

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

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

Upvotes: 3

Views: 47

Answers (1)

jezrael
jezrael

Reputation: 862751

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)

Upvotes: 2

Related Questions