嗨,對不起,我是 Pandas 的新手。我想知道如何輸入我試圖執行該功能的任何列的索引。我試過df[column].index了,但我一直IndexError在使用它。
對于背景關系,我試圖找到為每一列傳遞某個值(-10)的索引,即cd. 然后我必須對其進行插值以找到出現 -10 的確切值,即cd10.
非常感謝您!
for column in df.columns[1:]:
cd = HERdf.loc[df.iloc[: , df[column].index] <= -10].index[0]
cd10 = np.interp(x = -10 , xp = df.iloc[cd-1:cd 2 , df[column].index] , fp = df.iloc[cd-1:cd 2 , 0] )
print (cd10)
uj5u.com熱心網友回復:
我無法重現您的代碼,因為我不完全了解您的資料框的結構,也沒有完全理解插值部分,但我會嘗試僅根據您的描述提出解決問題的方法。
從描述中我猜你想從列名中獲取列的數字索引。如果我錯了,請糾正我,插值是問題的重要部分。
您可以通過將資料框的列索引轉換為串列來實作。
for column in df.columns[1:]:
column_index = df.columns.tolist().index(column)
print(f'column index of {column} is {column_index}')
另一個對我來說看起來更 Pythonic 的選項是列舉列:
for column_index, column in enumerate(df.columns[1:], start=1):
print(f'column index of {column} is {column_index}')
uj5u.com熱心網友回復:
從您的評論中,我只能猜測您正在尋找這樣的東西:
df = pd.DataFrame(data={'col1': [1, 2, 3], 'col2': [9.9, 10.1, 10]})
df.loc[df['col2'].eq(10), 'col1'].tolist()
它為您提供col1值作為col2等于的串列10。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495171.html
