Martan
Martan

Reputation: 605

Convert frequency table to raw data in Pandas

I have a sensor. For some reasons, the sensor like to record data like this:

>df
        obs  count
       -0.3    3
        0.9    2
        1.4    5

i.e. it first records observations and make a count table out of it. What I would like to do it convert this df into a series with raw observations. For example, I would like to end up with: [-0.3,-0.3,-0.3,0.9,0.9,1.4,1.4 ....]

Similar question asked for excel.

Upvotes: 0

Views: 976

Answers (1)

powerPixie
powerPixie

Reputation: 708

If your dataframe structure is like this one (or similar):

       obs  count
    0 -0.3      3
    1  0.9      2
    2  1.4      5

This is an option, using numpy.repeat:

   import numpy as np

   times = df['count'] 

   df2['obs'] = np.concatenate([np.repeat(df['obs'],times)])

   print(df2)

          obs
       0 -0.3
       1 -0.3
       2 -0.3
       3  0.9
       4  0.9
       5  1.4
       6  1.4
       7  1.4
       8  1.4
       9  1.4

Upvotes: 1

Related Questions