简体   繁体   中英

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:

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?

That's because the variable df in your for loop does not reference a value in your list. You are creating a variable df afresh each iteration of your loop.

You can assign via enumerate and pipe your function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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