0

I have the following data frame:

ID value freq
A a 0.1
A b 0.12
A c 0.19
B a 0.15
B b 0.2
B c 0.09
C a 0.39
C b 0.15
C c 0.01

and I would like to get the following

ID freq_a freq_b freq_c
A 0.1 0.12 0.19
B 0.15 0.2 0.09
C 0.39 0.15 0.01

Any ideas how to easily do this?

TRK
  • 85
  • 6

2 Answers2

0

using pivot:

df.pivot(index='ID', columns='value', values='freq').add_prefix('freq_').reset_index()

output:

>>
value ID  freq_a  freq_b  freq_c
0      A    0.10    0.12    0.19
1      B    0.15    0.20    0.09
2      C    0.39    0.15    0.01
eshirvana
  • 23,227
  • 3
  • 22
  • 38
0

Use pivot_table:

out = df.pivot_table('freq', 'ID', 'value').add_prefix('freq_') \
        .rename_axis(columns=None).reset_index()
print(out)

# Output
  ID  freq_a  freq_b  freq_c
0  A    0.10    0.12    0.19
1  B    0.15    0.20    0.09
2  C    0.39    0.15    0.01
Corralien
  • 109,409
  • 8
  • 28
  • 52