一些背景:我的代碼需要用戶輸入並將其應用於我的DF以刪除特定的行。這個過程可以重複用戶所希望的次數。不幸的是,我不知道如何使其保持正在發生變化更新我創建了while循環中我DF:在while循環期間更新熊貓DF(Python3,Pandas)
data = ({'hello':['the man','is a','good guy']})
df = pd.DataFrame(data)
def func():
while True:
n = input('Words: ')
if n == "Done":
break
elif n != "Done":
pattern = '^'+''.join('(?=.*{})'.format(word) for word in n.split())
df[df['hello'].str.contains(pattern)==False]
如何在每一個循環,這樣的變化的結束時更新的DF被保持放?
使用'loc'開始
func
: 'df.loc [df ['hello']。str.contains(pattern)== False,'col'] = newVal' – EdChum 2014-09-30 07:18:17不確定這段代碼是如何工作的?如果你不介意,你可以多解釋一下嗎? – user3682157 2014-09-30 14:26:05
'loc'使用基於標籤的索引查看文檔:http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing-loc-iloc-and-ix – EdChum 2014-09-30 15:17:16