![](/img/trans.png)
[英]Filling each row of one column of a DataFrame with different values (a random distribution)
[英]Filling in Dataframe given one column and one row as coordinates
如果我有一個部分空的 dataframe 例如:
col1 col2 col3 col4 col5
4
3
2
1
4 3 2 1 0
我想用坐標列表填充空單元格,例如:
col1 col2 col3 col4 col5
[4,4] [3,4] [2,4] [1,4] 4
[4,3] [3,3] [2,3] [1,3] 3
[4,2] [3,2] [2,2] [1,2] 2
[4,1] [3,1] [2,1] [1,1] 1
4 3 2 1 0
謝謝您的幫助!
嘗試使用axis
= 0 和 1 的bfill
,然后將其相加並fillna
df = df.fillna(df.bfill().applymap(lambda x : [x]) + df.bfill(1).applymap(lambda x : [x]))
Out[431]:
col1 col2 col3 col4 col5
0 [4.0, 4.0] [3.0, 4.0] [2.0, 4.0] [1.0, 4.0] 4
1 [4.0, 3.0] [3.0, 3.0] [2.0, 3.0] [1.0, 3.0] 3
2 [4.0, 2.0] [3.0, 2.0] [2.0, 2.0] [1.0, 2.0] 2
3 [4.0, 1.0] [3.0, 1.0] [2.0, 1.0] [1.0, 1.0] 1
4 4 3 2 1 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.