I defined a function related to the data frame, but the actual data does not change.

Asked 2 years ago, Updated 2 years ago, 56 views

In the Pandas data frame, we created a function to delete all the lower lines of a column when the value of a column appears nan.

def shorter(df,column):

    for i in range(len(df)):
        if pd.isna(df[column])[i] == True:
           df = df.loc[0:i-1]
           break          

And then I put that data frame in df and spin it, but there's no change in the actual data frame.

For example, if the data frame name is df2022 (already defined before adding it to the function), and the column name is 'Determine'

Even if you do shorter (df2022, 'judgment')

df2022 does not change. Why is he doing this? I want to get the result of changing the actual data.

dataframe pandas function

2022-09-20 08:43

1 Answers

df = df.loc[0:i-1]

What happens here is not to change the df itself, but to create a new df.loc[0:i-1] and assign it to the name df. The newly created df is irrelevant to the df outside the function. So it doesn't change.

def shorter(df,column):
    for i in range(len(df)):
        if pd.isna(df[column])[i] == True:
           return df.loc[0:i-1]
    return df



df2022 = shorter (df2022, "judgment")

I think this will work.


2022-09-20 08:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.