Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Apply function to data frame and make output a separate df pandas

I have a data frame

cat input.csv
dwelling,wall,weather,occ,height,temp
5,2,Ldn,Pen,154.7,23.4
5,4,Ldn,Pen,172.4,28.7
3,4,Ldn,Pen,183.5,21.2
3,4,Ldn,Pen,190.2,30.3

To which I’m trying to apply the following function:

input_df = pd.read_csv('input.csv')


def folder_column(row):
    if row['dwelling'] == 5 and row['wall'] == 2:
        return 'folder1'
    elif row['dwelling'] == 3 and row['wall'] == 4:
        return 'folder2'
    else:
        return 0

I want to run the function on the input dataset and store the output in a separate data frame using something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

temp_df = pd.DataFrame()
temp_df = input_df['archetype_folder'] = input_df.apply(folder_column, axis=1)

But when I do this I only get the newly created ‘archetype_folder’ in the temp_df, when I would like all the original columns from the input_df. Can anyone help? Note that I don’t want to add the new column ‘archetype_folder’ to the original, input_df. I’ve also tried this:

temp_df = input_df

temp_df['archetype_folder'] = temp_df.apply(folder_column, axis=1)

But when I run the second command both input_df and temp_df end up with the new column?

Any help is appreciated!

>Solution :

Use Dataframe.copy :

temp_df = input_df.copy()

temp_df['archetype_folder'] = temp_df.apply(folder_column, axis=1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading