如何基于数字列值将Pandas数据框中的一行转换为多行?

时间:2019-07-24 12:17:43

标签: python pandas dataframe

我想基于列的值(NumRealVisits)将Dataframe输出中的行数相乘。

(例如,如果NumRealVisits是23,我想将该行重复23次。)

当我在for循环中尝试以下代码时:

s = df_content.set_index(['NumRealVisits']).stack()
df_content= (s.loc[s.index.repeat(s)].reset_index().drop(0, axis=1))

我收到以下错误:

IndexError: list index out of range

下面是我现在所拥有的以及我想要实现的目标的一个示例。

在:

ContentId   OrgId   Type    NumRealVisits   TotalTime
4063        6767    Topic   2               27
4062        6767    Topic   1               57
4061        6767    Topic   2               41
4004        6764    Topic   2               24

出局:

ContentId   OrgId   Type    NumRealVisits   TotalTime
4063        6767    Topic   2               27
4063        6767    Topic   2               27
4062        6767    Topic   1               57
4061        6767    Topic   2               41
4061        6767    Topic   2               41
4004        6764    Topic   2               24
4004        6764    Topic   2               24

任何帮助表示感谢,谢谢您

0 个答案:

没有答案