我有一個如下所示的資料框:
Name X Y
0 A False True
1 B True True
2 C True False
我想創建一個函式,例如:
example_function("A") = "A is in Y"
example_function("B") = "B is in X and Y"
example_function("C") = "C is in X"
這是我目前的代碼(不正確,看起來效率不高):
def example_function(name):
for name in df['Name']:
if df['X'][name] == True and df['Y'][name] == False:
print(str(name) "is in X")
elif df['X'][name] == False and df['Y'][name] == True:
print(str(name) "is in Y")
else:
print(str(name) "is in X and Y")
我最終想添加更多布爾列,因此它需要可擴展。我怎樣才能做到這一點?創建字典而不是資料框會更好嗎?
謝謝!
uj5u.com熱心網友回復:
如果你真的想要一個功能,你可以這樣做:
def example_function(label):
s = df.set_index('Name').loc[label]
l = s[s].index.to_list()
return f'{label} is in {" and ".join(l)}'
example_function('A')
'A is in Y'
example_function('B')
'B is in X and Y'
您還可以將所有解決方案計算為字典:
s = (df.set_index('Name').replace({False: pd.NA}).stack()
.reset_index(level=0)['Name']
)
out = s.index.groupby(s)
輸出:
{'A': ['Y'], 'B': ['X', 'Y'], 'C': ['X']}
uj5u.com熱心網友回復:
我認為您可以使用 DataFrame,可以使用以下函式獲得相同的輸出:
def func (name, df):
# some checks to verify that the name is actually in the df
occurrences_name = np.sum(df['Name'] == name)
if occurrences_name == 0:
raise ValueError('Name not found')
elif occurrences_name > 1:
raise ValueError('More than one name found')
# get the index corresponding to the name you're looking for
# and select the corresponding row
index = df[df['Name'] == name].index[0]
row = df.drop(['Name'], axis=1).iloc[index]
outstring = '{} is in '.format(name)
for i in range(len(row)):
if row[i] == True:
if i != 0: outstring = ', '
outstring = '{}'.format(row.index[i])
return outstring
當然,您可以根據您的 df 的特定形狀進行調整,我假設包含名稱的列實際上是“名稱”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/449217.html
