我能夠在 SO 的幫助下完成這項作業:
# Input string changes but the one I need is always passed in
input_string = "random string"
df.loc[input_string, df.drop(input_string).eq(111).all()] = 111
上面的代碼本質上采用一列并檢查該列中資料幀的所有單元格值input_string是否為 111,如果是,則將該列設定為 111。
我該如何連續執行此操作?
uj5u.com熱心網友回復:
我認為你的邏輯是倒退的。您當前所做的實際上是洗掉一行并檢查其他行是否符合您的條件。見下文:
#do column checking
df = pd.DataFrame({'a':[111]*10,'b':[111]*10})
df_col = df.copy()
input_col = 'a'
df_col[input_col] = [222]*len(df.index) #distort input - should be removed in next step
df_col.loc[df_col.drop(input_col,axis=1).eq(111).all(1), input_col] = 111
#do row checking
df_row = df.copy()
input_row = 2
df_row.iloc[input_row] = [222]*len(df.columns) #distort input - should be removed in next step
df_row.loc[input_row,df_row.drop(input_row).eq(111).all()] = 111
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/381271.html
標籤:Python 熊猫 数据框 python-3.7 位置
上一篇:列中的逗號分隔值作為熊貓中的行
