17. Python - Pandas - Renaming/Combining
Changing names:
// Columns:
data_frame.rename(columns = {'OLDNAME' : 'NEWNAME'})
// can add multiple renames by adding a comma
// Index:
data_frame.rename(index = {0: 'first index' , 1: '2nd index'})
// set_index() is more convenient.
// Axis Index
data_frame.rename_axis("AXISNAME_A", axis = 'rows').rename_axis("AXISNAME_B", axis = "columns")
// renames whole 2 axis, the row and the column
Combining:
// concat():
pd.concat([data_frame_1, data_frame_2])
// combines two data frames who have the same columns(fields) into one data frame
// basically places both data under the same columns
// join():
left = data_frame_1.set_index(['FIRST INDEX' , 'SECOND INDEX'])
right = data_frame_2.sex_index(['FIRST INDEX' , 'SECOND INDEX'])
left.join(right, lsuffix = '_FIRSTDATA', rsuffix='_2NDDATA')
// creates a new data frame and combine 2 data frames data if they have the same indexes and then merges them into a long row, the suffix lets us differentiate between 2 data in the row by making every column below to each data frame.
// if the data have different column values, we wont need to add suffix because we cant mix them up since they are not the same.
// merge():
Comments
Post a Comment