您好我正在嘗試提取三行資料。第 0 行第 1 行和標題為“庫存”的行。
我認為最好的方法是找到庫存的行數并使用 iloc 決議日期。但是,我收到一個對許多索引器說的錯誤。任何幫助,將不勝感激
df.columns=df.iloc[1]
cols = df.columns.tolist()
A =df.loc[df[cols[0]].isin(['Inventories'])].index.tolist()
df = df.iloc[[0,1,[A]]]
我也試過
df = df.iloc[[0,1,A]]
另請注意 A 回傳 56,如果我用 56 替換 A
df = df.iloc[[0,1,56]]
我得到了想要的結果。
任何幫助,將不勝感激。
uj5u.com熱心網友回復:
對于匹配條件的位置 use Series.argmax,所以可以A不添加[]to DataFrame.iloc,如果總是匹配條件,它會很好地作業:
A = df[cols[0]].eq('Inventories').argmax()
df = df.iloc[[0,1,A]]
另一個想法是通過按位添加條件OR來|測驗前 2 行:
df = pd.DataFrame({'col' : [100,10,'s','Inventories',1,10,100]})
df.index = 10
print (df)
col
10 100
11 10
12 s
13 Inventories
14 1
15 10
16 100
df = df[np.in1d(np.arange(len(df)), [0,1]) | df.iloc[:, 0].eq('Inventories')]
print (df)
col
10 100
11 10
13 Inventories
或按位置和條件連接過濾的行:
df = pd.concat([df.iloc[[0, 1]], df[df.iloc[:, 0].eq('Inventories')]])
print (df)
col
10 100
11 10
13 Inventories
uj5u.com熱心網友回復:
IIC 您想要在索引中提取 3 個特定值(可以是索引號或字串)。這將允許您設定在參考索引時要拉回的值。
df = pd.DataFrame({
'Column' : [1, 2, 3, 4, 5],
'index' : [0, 1, 'Test', 'Inventories', 4]
})
df = df.set_index('index')
df.loc[[0, 1, 'Inventories']]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/491347.html
上一篇:Pandas-連接資料框
下一篇:熊貓中的編號序列
