我有一個具有這種結構的資料框:
| 一種 | 索引器 | attr1_rank | attr2_rank | attr3_rank | attr4_rank | ... | attrn_rank |
|---|---|---|---|---|---|---|---|
| 磷 | 1 | 2 | 1 | 3 | 4 | ... | n |
| 小號 | 2 | 1 | 2 | 4 | 3 | ... | n |
如何添加具有下一個預期值的列,其中return_value基于值的列名稱indexer應indexer與 in 進行比較attr1_rank并attr2_rank回傳其標題:
| 回傳值 |
|---|
| 屬性2 |
| 屬性2 |
我有這個代碼,但回傳索引錯誤。vartmp代表indexer
index_value = self._data.iloc[row, [2, 4]] == int(tmp)
col_name = self._data.columns[index_value]
col_name = col_name.removesuffix('_rank')
self._data.iloc[row, column 3] = col_name # assuming that 'return_value' column is 3 positions to the right
@JAV 解決方案的更新,這是我適合的代碼
def setData(self, index, value, role=Qt.EditRole):
based_columns = [6, 8, 10, 12]
if role == Qt.EditRole:
row = index.row()
column = index.column()
tmp = str(value)
if column in based_columns:
if column == 6 and tmp in self._data.columns.values.tolist():
index_no = self._data.columns.get_loc(tmp)
self._data.iloc[row, column 1] = self._data.iloc[row, index_no]
self._data.iloc[row, column] = tmp
elif column in [8, 10, 12]:
self._data.iloc[row, column 1] = self._data.apply(self.index_match(row), axis=1)
self._data.iloc[row, column] = tmp
self.dataChanged.emit(index, index)
def index_match(self, row):
for col in row[97:].index:
if row[col] == row['indexer']:
return col[:-5]
Traceback (most recent call last):
File "helper_classes.py", line 171, in setData
self._data.iloc[row, column 1] = self._data.apply(self.index_match(row), axis=1)
File "helper_classes.py", line 176, in index_match
for col in row[97:].index:
TypeError: 'int' object is not subscriptable
uj5u.com熱心網友回復:
如果要回圈的列數未定義,則可以使用它:
def index_match(row):
for col in row[1:].index:
if row[col] == row['indexer']:
return col[:-5] # trimming off _rank
df['return_value'] = df.apply(index_match, axis=1)
uj5u.com熱心網友回復:
這是一個為你做這件事的班輪。
df['return_value']=df.apply(lambda x: 'attr2' if x['indexer']==x['attr2_rank'] else ('attr1' if x['indexer']==x['attr1_rank'] else None ), axis =1)
如果列數很大的解決方案:
def get_return_val(x):
vals=set(x.loc[x.indexer == x].index)- {'indexer'}
if len(vals):
return [x.rstrip('_rank') for x in vals ][0]
else:
return None
df['return_value'] = df.apply(get_return_val, axis=1)
uj5u.com熱心網友回復:
df.apply(lambda x: list((dictionary:=x[[index for index in x.index if '_rank' in index]].to_dict()).keys())[list(dictionary.values()).index(x['indexer'])].replace("_rank",""), axis=1)
或者
df.apply(lambda x: list((dictionary:=x[[index for index in x.index if '_rank' in index]].to_dict()).keys())[list(dictionary.values()).index(x['indexer'])][:-5], axis=1)
或者
df.apply(lambda x: list((dictionary:=x.drop(['A', 'indexer']).to_dict()).keys())[list(dictionary.values()).index(x['indexer'])][:-5], axis=1)
uj5u.com熱心網友回復:
對于我的功能來說,這比我想象的要容易。
for x in range(initial_column, end_column):
if self._data.iloc[row, x] == int(tmp):
index_value = x
break
col_name = self._data.columns[index_value]
col_name = col_name.removesuffix('_rank')
self._data.iloc[row, column 1] = col_name
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474546.html
上一篇:從其他pandas列創建新列
