[英]Python pandas: Why doesnt `del df.loc[:,column_name]` work? (Even though `del df[column_name]` does?
[英]Get df.loc['column_name'] when column_name is a variable in Python Pandas
我有一個變量,我稱之為“響應”,用戶手動輸入“Apple”或“Tree”這些詞。 然后我想根據用戶回答 select 名稱=“Apple”或“Tree”的列。 我怎樣才能做到這一點? 像 df.loc[response] 這樣的東西?
任何幫助將不勝感激,謝謝
您可以簡單地訪問具有response
變量的列,例如
df[response]
df[response]
將根據您在響應變量中的內容評估為df['Apple']
或df['tree']
最簡單的方法是:
df[response]
其中 response 是變量,可以是“Apple”或“Tree”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.