我有一个非常简单的for循环:
"{ Flags: 5 }"
当我运行for循环时,没有任何过滤器。
但是,如果我手动将其写下来:
## Keep or Drop Rows from Ad Servers
dataframes = [atlas_df, flashtalking_df, innovid_df, ias_viewability_df, ias_fraud_df]
for df in dataframes:
df = df[df['Placement Name'].str.contains("»")]
过滤器有效。
关于我缺少的任何想法?
答案 0 :(得分:3)
您正在使用迭代器,您需要通过在列表中使用索引来引用原始df:
for i in range(len(dataframes)):
df = dataframes[i]
dataframes[i] = df[df['Placement Name'].str.contains("»")]
这样就可以修改列表中的原始df
示例:
In [108]:
l = list('abcd')
for i in range(len(l)):
l[i] = 'new_' + l[i]
Out[108]:
['new_a', 'new_b', 'new_c', 'new_d']
对战:
In [110]:
l = list('abcd')
for x in l:
x = 'new_' + x
l
Out[110]:
['a', 'b', 'c', 'd']
所以你看到后者在语义上与你的代码相同,从不修改列表中的原始元素而另一个元素
答案 1 :(得分:1)
您可以使用列表理解 - 输出是已过滤的Dataframes
列表:
dataframes = [df[df['Placement Name'].str.contains(u"»")] for df in dataframes]
样品:
atlas_df = pd.DataFrame({'Placement Name':['deu_gathf»', 'deu_gahf', 'fra_gagg'],
'another_col':[1,2,3]})
flashtalking_df = pd.DataFrame({'Placement Name':['deu_gahf»','fra_ga', 'deu_gatt'],
'another_col':[4,5,6]})
dataframes = [atlas_df, flashtalking_df]
print (dataframes)
[ Placement Name another_col
0 deu_gathf» 1
1 deu_gahf 2
2 fra_gagg 3, Placement Name another_col
0 deu_gahf» 4
1 fra_ga 5
2 deu_gatt 6]
dataframes = [df[df['Placement Name'].str.contains(u"»")] for df in dataframes]
print (dataframes)
[ Placement Name another_col
0 deu_gathf» 1, Placement Name another_col
0 deu_gahf» 4]