Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Pandas equivalent to tidyr's uncount?

Tags:

python

pandas

r

Let's assume we have a table with groupings of variable and their frequencies:

In R:

> df

# A tibble: 3 x 3
  Cough Fever cases
  <lgl> <lgl> <dbl>
1 TRUE  FALSE     1
2 FALSE FALSE     2
3 TRUE  TRUE      3

Then we could use tidyr::uncount to get a dataframe with the individual cases:

> uncount(df, cases)

# A tibble: 6 x 2
  Cough Fever
  <lgl> <lgl>
1 TRUE  FALSE
2 FALSE FALSE
3 FALSE FALSE
4 TRUE  TRUE 
5 TRUE  TRUE 
6 TRUE  TRUE 

Is there an equivalent in Python/Pandas?

like image 570
Bubblbu Avatar asked Oct 28 '25 15:10

Bubblbu


2 Answers

In addition to the other solutions, you could combine take, repeat and drop:

import pandas as pd
df = pd.DataFrame({'Cough': [True, False, True],
                   'Fever': [False, False, True],
                   'cases': [1, 2, 3]})

df.take(df.index.repeat(df.cases)).drop(columns="cases")


    Cough   Fever
0   True    False
1   False   False
1   False   False
2   True    True
2   True    True
2   True    True

You can also preselect the columns before indexing on the positions:

df.loc(axis=1)[:'Fever'].take(df.index.repeat(df.cases))
   Cough  Fever
0   True  False
1  False  False
1  False  False
2   True   True
2   True   True
2   True   True
like image 182
sammywemmy Avatar answered Oct 31 '25 05:10

sammywemmy


You have a row index and repeat it according to the counts, for example in R you can do:

df[rep(1:nrow(df),df$cases),]

first to get a data like yours:

df = pd.DataFrame({'x':[1,1,2,2,2,2],'y':[0,1,0,1,1,1]})
counts = df.groupby(['x','y']).size().reset_index()
counts.columns = ['x','y','n']

    x   y   n
0   1   0   1
1   1   1   1
2   2   0   1
3   2   1   3

Then:

counts.iloc[np.repeat(np.arange(len(counts)),counts.n),:2]

    x   y
0   1   0
1   1   1
2   2   0
3   2   1
3   2   1
3   2   1
like image 27
StupidWolf Avatar answered Oct 31 '25 04:10

StupidWolf



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!