DEV Community

GharamElhendy
GharamElhendy

Posted on

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

Top comments (0)