Appending Data in Pandas (+Numpy) When Working with Two Data Sets That Have the Same Attributes (Columns)

Explanation

Let’s assume we have 2 sets of data in 2 different csv files. For example, data that includes petal length, width, thickness, etc. for a certain kind of flower that can be found in 2 colors.

Let’s assume that these 2 colors are red and white.

While they’re of different colors, any given flower would have the same attributes, except for the color. So, we can make that distinction in the data set that combines both.

Importing and Parsing

import numpy as np
import pandas as pd

red_df = pd.read_csv(‘red_flowers.csv’)
white_df = pd.read_csv(‘white_flowers.csv’)

We’ll create a dataframe for both color arrays

color_red = np.repeat(‘red’, red_df.shape[0])
color_white = np.repeat(‘white’, white_df.shape[0])

Then, we’ll add arrays to boh dataframes by setting a new column named “color” to the corresponding array.

red_df[‘color’] = color_red
white_df[‘color’] = color_white

Then, we’ll append the dataframes and view the file to make sure we’ve done it successfully.

flowers_df = red_df.append(white_df)
flowers_df.head()

Then, we’ll save the new combined dataframe to a .csv file.

flowers_df.to_csv(‘flowers_edited.csv’, index=False)

Finally, we can check the file to make sure the two datasets were combined successfully by making sure that the rows are the sum of the two sets and that the columns are the initial number of columns in each data set + 1.

flowers_df.shape

_I.e: If each data set had 12 columns, and the red flowers set had 55, while the white flowers set had 45 rows, then the flowers_df.shape attribute should result in:

(100, 13)

原文链接:Appending Data in Pandas (+Numpy) When Working with Two Data Sets That Have the Same Attributes (Columns)

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
I try to give up the dream just a dream.
努力了才叫梦想
评论 抢沙发

请登录后发表评论

    暂无评论内容