![](/img/trans.png)
[英]Pandas select rows when column value within range from another row column value with group filter
[英]Select a range of column base on value of another column in pandas
我的數據集看起來像這樣(第一行是標題)
0 1 2 3 4 5
1 3 4 6 2 3
3 8 9 3 2 4
2 2 3 2 1 2
我想基於列[5]選擇數據集的列范圍,例如:
1 3 4
3 8 9 3
2 2
我已經嘗試了以下方法,但是沒有用:
df.iloc[:,0:df['5'].values]
我們試試吧:
df.apply(lambda x: x[:x.iloc[5]], 1)
輸出:
0 1 2 3
0 1.0 3.0 4.0 NaN
1 3.0 8.0 9.0 3.0
2 2.0 2.0 NaN NaN
重新創建您的數據框
df=pd.DataFrame([x[:x[5]] for x in df.values]).fillna(0)
df
Out[184]:
0 1 2 3
0 1 3 4.0 0.0
1 3 8 9.0 3.0
2 2 2 0.0 0.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.