相关疑难解决方法(0)

复制Pandas中的行

我的pandas数据框看起来像这样:

   Person  ID   ZipCode   Gender
0  12345   882  38182     Female
1  32917   271  88172     Male
2  18273   552  90291     Female
Run Code Online (Sandbox Code Playgroud)

我想复制每一行3次,如:

   Person  ID   ZipCode   Gender
0  12345   882  38182     Female
0  12345   882  38182     Female
0  12345   882  38182     Female
1  32917   271  88172     Male
1  32917   271  88172     Male
1  32917   271  88172     Male
2  18273   552  90291     Female
2  18273   552  90291     Female
2  18273   552  90291     Female
Run Code Online (Sandbox Code Playgroud)

当然,重置索引所以它是:

0
1
2
Run Code Online (Sandbox Code Playgroud)

我尝试过如下解决方案:

pd.concat([df[:5]]*3, ignore_index=True)
Run Code Online (Sandbox Code Playgroud)

和:

df.reindex(np.repeat(df.index.values, df['ID']), …
Run Code Online (Sandbox Code Playgroud)

python dataset dataframe pandas

11
推荐指数
4
解决办法
8734
查看次数

pandas:对于df复制行中的每一行N次略有变化

所以我有一个像这样的DataFrame:

   N    start
1  1    08/01/2014 9:30:02
2  1    08/01/2014 10:30:02 
3  2    08/01/2014 12:30:02
4  3    08/01/2014 4:30:02
Run Code Online (Sandbox Code Playgroud)

我需要复制每一行N次,每次增加一小时,如下所示:

   N    start
1  1    08/01/2014 9:30:02
2  1    08/01/2014 10:30:02 
3  2    08/01/2014 12:30:02
3  2    08/01/2014 13:30:02
4  3    08/01/2014 4:30:02
4  3    08/01/2014 5:30:02
4  3    08/01/2014 6:30:02
Run Code Online (Sandbox Code Playgroud)

我怎么能在熊猫里做到这一点?

python pandas

8
推荐指数
1
解决办法
5073
查看次数

重复数据帧的行

我正在尝试重复数据帧的行.这是我的原始数据:

pd.DataFrame([
        {'col1': 1, 'col2': 11, 'col3': [1, 2] },
        {'col1': 2, 'col2': 22, 'col3': [1, 2, 3] },
        {'col1': 3, 'col2': 33, 'col3': [1] },
        {'col1': 4, 'col2': 44, 'col3': [1, 2, 3, 4] },
    ])
Run Code Online (Sandbox Code Playgroud)

这给了我

   col1  col2          col3
0     1    11        [1, 2]
1     2    22     [1, 2, 3]
2     3    33           [1]
3     4    44  [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

我想根据col3中数组的长度重复行,即我想获得像这样的数据帧.

   col1  col2
0     1    11
1     1    11
2     2    22
3     2    22
4     2 …
Run Code Online (Sandbox Code Playgroud)

python pandas python-3.6

5
推荐指数
1
解决办法
210
查看次数

标签 统计

pandas ×3

python ×3

dataframe ×1

dataset ×1

python-3.6 ×1