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')

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])

Appending the two dataframes

population_df = males_df.append(females_df)

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)

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

population_df.shape

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

21