0

I have 5 dataframes A through E and I'd like to apply the same process to all frames. I've executed a loop but it doesn't overwrite the original dataframes I'd like to change, they result as identical to the frames I've fed to the loop:

frames = [A,B,C,D,E]

for df in frames:
    df = df[df.columns.drop(list(df.filter(regex='Unnamed')))] # Drop columns with "Unnamed" in column name
    df = df.apply(lambda x: x.astype(str).str.upper()) # Convert columns to caps
    df['Unique Name'] = df['Name'] + ' ' + df['Gender'] + ' ' + df['Classification'] + ' ' + df['Silhouette']

The exact same code works if I run it individually for each dataframe.

2 Answers 2

1

You can try following:

frames = [A,B,C,D,E]
def update(df):
       df = df[df.columns.drop(list(df.filter(regex='Unnamed')))] # Drop columns with "Unnamed" in column name
       df = df.apply(lambda x: x.astype(str).str.upper()) # Convert columns to caps
       df['Unique Name'] = df['Name'] + ' ' + df['Gender'] + ' ' + df['Classification'] + ' ' + df['Silhouette']
       return df
for i in frames:
  i = update(i)
   

The issue with your code is you are just overwrite df, instead of updating your actual Dataframes.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use frames[i] inside the loop instead of df:

for i in range(len(frames)):
  frames[i] = frames[i][frames[i].columns. ...
  ...

Thus you overwrite the data frames inside the list. An easy adaptation of your code:

for i, df in zip(range(len(frames)), frames):
  df = df[df.columns.drop(list(df.filter(regex='Unnamed')))] # Drop columns with "Unnamed" in column name
  df = df.apply(lambda x: x.astype(str).str.upper()) # Convert columns to caps
  df['Unique Name'] = df['Name'] + ' ' + df['Gender'] + ' ' + df['Classification'] + ' ' + df['Silhouette']

  frames[i] = df

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.