一些背景:我的代码需要用户输入并将其应用于我的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