我正在使用这样加载的熊猫数据框:
data = pd.read_csv("population_stats.txt", sep=" ", header=None)
0 1 2
0 100 200 300
1 400 500 600
2 700 800 900
3 420 320 652
4 125 258 852
我想用cylic数据填充它,意思是将数据重复写入直到恒定的256行计数。因此,最终的数据帧看起来如下:
0 1 2
0 100 200 300
1 400 500 600
2 700 800 900
3 420 320 652
4 125 258 852
5 100 200 300
6 400 500 600
7 700 800 900
8 420 320 652
9 125 258 852
10 100 200 300
11 400 500 600
12 700 800 900
13 420 320 652
14 125 258 852
-- .. .. ..
256 <> <> <>
我想知道是否有一个快速的熊猫技巧可以做到这一点,从而避免编写任何纯python循环。
答案 0 :(得分:1)
尝试一下:
import numpy as np
import pandas as pd
larger = np.tile(df, (52,1)) # use np.tile to repeat the df
# the repeated version needs clipping to 256 rows as it has more than that
clipped = pd.DataFrame(larger).head(256)