DEV Community

GharamElhendy
GharamElhendy

Posted on

1

Appending Two DataFrames in Pandas

Let's say we have two dataframes with the same attributes and they share a physical attribute that may vary. We can combine both dataframes and differentiate using this physical attribute to make handling csv files easier.

For example, if we have descriptions of colors of skin, hair, and eyes for two dataframes, one for males and one for females, we can add a column at the end that signifies whether the attributes in a certain row belong to a male or female, all in the same dataframe (population_df) instead of 2.

Importing and reading the files

Import pandas as pd
males_df = pd.read_csv('males.csv')
females_df = pd.read_csv('females.csv')
Enter fullscreen mode Exit fullscreen mode

Adding a last column with the attribute used for distinction

gender_male = np.repeat('male', males_df.shape[0])
gender_female = np.repeat('female', females_df.shape[0])
Enter fullscreen mode Exit fullscreen mode

Appending the two dataframes

population_df = males_df.append(females_df)
Enter fullscreen mode Exit fullscreen mode

Saving the combined dataset with a False index in order not to save the file with the unnamed column

population_df.to_csv('Filename.csv'), index=False)
Enter fullscreen mode Exit fullscreen mode

Note: You can make sure that you've successfully appended your two dataframes by using .shape

population_df.shape
Enter fullscreen mode Exit fullscreen mode

If the number is the sum of the two dataframe counts, proceed with your work

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

If you found this post helpful, please leave a ❤️ or a friendly comment below!

Okay