简体   繁体   English

应用循环后,未更改原始数据帧

[英]No changes to original dataframe after applying loop

I have a list of dataframes such that 我有一个数据框列表,这样

df_lst = [df1, df2]

I also created a function which removes the rows with '0' in the dataframe: 我还创建了一个函数来删除数据框中带有“ 0”的行:

def dropzeros(df):
    newdf = df[df['x']!=0.0]
    return newdf

I tried applying this through a loop and placed an assignment variable within the loop, but the original dataframe remained unchanged even after running the loop. 我尝试通过循环应用此方法,并在循环中放置了一个赋值变量,但是即使在运行循环后,原始数据帧也保持不变。

for df in df_lst:
    df = dropzeros(df)

I also tried using list comprehensions to go about it 我也尝试使用列表理解来解决这个问题

df_lst = [dropzeros(df) for df in df_lst]

I know the function works since when i apply print(len(df)) before and after the command dropzeros(df) there was a drop in the len, however, may I know how might I go about this problem such that my original dataframe is altered after running the loop? 我知道该函数有效,因为当我在命令dropzeros(df)之前和之后应用print(len(df))时,len下降了,但是,我可能会知道如何解决这个问题,例如我的原始数据帧运行循环后更改?

That's because the variable df in your for loop does not reference a value in your list. 这是因为for循环中的变量df没有引用列表中的值。 You are creating a variable df afresh each iteration of your loop. 您将在循环的每个迭代中重新创建变量df

You can assign via enumerate and pipe your function: 您可以通过enumeratepipe分配功能:

for idx, df in enumerate(df_lst):
    df_lst[idx] = df.pipe(dropzeros)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM