我正在使用 pandas 數(shù)據(jù)框,我以這種方式加載:data = pd.read_csv("population_stats.txt", sep=" ", header=None) 0 1 20 100 200 3001 400 500 6002 700 800 9003 420 320 6524 125 258 852我想用循環(huán)數(shù)據(jù)填充它,這意味著重復(fù)數(shù)據(jù)直到恒定的 256 行計數(shù)。所以,最終的數(shù)據(jù)框看起來: 0 1 20 100 200 3001 400 500 6002 700 800 9003 420 320 6524 125 258 8525 100 200 3006 400 500 6007 700 800 9008 420 320 6529 125 258 85210 100 200 30011 400 500 60012 700 800 90013 420 320 65214 125 258 852-- .. .. ..256 <> <> <>我想知道是否有一個快速的 pandas 技巧可以做到這一點(diǎn),這將避免編寫任何純 python 循環(huán)。
1 回答

12345678_0001
TA貢獻(xiàn)1802條經(jīng)驗 獲得超5個贊
嘗試這個:
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)
添加回答
舉報
0/150
提交
取消