我想在現(xiàn)有數(shù)據(jù)框中創(chuàng)建一個列,其值為“頂部”和“底部”,要注意的是,數(shù)據(jù)框的大小根據(jù)計(jì)算而變化。例如:if dataframe has 4 rows then:Top - Row1Bottom - Row2Top - Row3Bottom - Row4我將始終擁有偶數(shù)行。請?zhí)岢鼋鉀Q方案,謝謝!
1 回答

泛舟湖上清波郎朗
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個贊
我不知道你的數(shù)據(jù)到底如何,但你可以嘗試這樣的事情:
假設(shè)數(shù)據(jù):
data = {'A':['12345','12345','67890','12345', '', '12345']}
df = pd.DataFrame(data)
創(chuàng)建新列
df['B']= ['Top' if i%2==0 else 'Bottom' for i,_ in df.iterrows() ]
df
A B
0 12345 Top
1 12345 Bottom
2 67890 Top
3 12345 Bottom
4 12345 Top
添加回答
舉報
0/150
提交
取消