有一只Pandas相当于蒂蒂尔的数量吗?

2024-06-15 15:28:23 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我们有一个包含变量分组及其频率的表:

在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

然后,我们可以使用tidyr::uncount获得一个包含各个案例的数据帧:

> 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 

Python/Pandas中是否有一个等价物


Tags: 数据falsetruepandasdf案例fever频率
3条回答

我还没有在Python中找到一个等价的函数,但这是可行的

df2 = df.pop('cases')
df = pd.DataFrame(df.values.repeat(df2, axis=0), columns=df.columns)

df['cases']被传递给df2,然后您创建一个新的数据帧,根据df2中的计数重复原始数据帧中的元素。如果有帮助,请告诉我

您有一个行索引,并根据计数重复它,例如在R中,您可以执行以下操作:

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

首先要获得像您这样的数据:

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

然后:

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

除了其他解决方案之外,您还可以组合takerepeatdrop

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

相关问题 更多 >