Home > other >  separating values ​between rows with pandas
separating values ​between rows with pandas

Time:05-24

I want to separate values in "alpha" column like this

Start:

alpha beta gamma
A 1 0
A 1 1
B 1 0
B 1 1
B 1 0
C 1 1

End:

alpha beta gamma
A 1 0
A 1 1
X X X
B 1 0
B 1 1
B 1 0
X X X
C 1 1

Thanks for help <3

CodePudding user response:

You can try

out = (df.groupby('alpha')
       .apply(lambda g: pd.concat([g, pd.DataFrame([['X', 'X', 'X']], columns=df.columns)]))
       .reset_index(drop=True))
print(out)

  alpha beta gamma
0     A    1     0
1     A    1     1
2     X    X     X
3     B    1     0
4     B    1     1
5     B    1     0
6     X    X     X
7     C    1     1
8     X    X     X

CodePudding user response:

Assuming a range index as in the example, you can use:

# get indices in between 2 groups
idx = df['alpha'].ne(df['alpha'].shift(-1).ffill())

df2 = pd.concat([df, df[idx].assign(**{c: 'X' for c in df})]).sort_index(kind='stable')

Or without groupby and sort_index:

idx = df['alpha'].ne(df['alpha'].shift(-1).ffill())
df2 = df.loc[df.index.repeat(idx 1)]
df2.loc[df2.index.duplicated()] = 'X'

output:

  alpha beta gamma
0     A    1     0
1     A    1     1
1     X    X     X
2     B    1     0
3     B    1     1
4     B    1     0
4     X    X     X
5     C    1     1

NB. add reset_index(drop=True) to get a new index

CodePudding user response:

You can do:

dfx = pd.DataFrame({'alpha':['X'],'beta':['X'],'gamma':['X']})
df = df.groupby('alpha',as_index=False).apply(lambda x:x.append(dfx)).reset_index(drop=True)

Output:

   alpha  beta  gamma
0     A    1      0
1     A    1      1
2     X    X      X
3     B    1      0
4     B    1      1
5     B    1      0
6     X    X      X
7     C    1      1
8     X    X      X

To avoid adding a [X, X, X] at the end you can check the index first like:

df.groupby('alpha',as_index=False).apply(
    lambda x:x.append(dfx) 
    if x.index[-1] != df.index[-1] else x).reset_index(drop=True)
  • Related