Jana's solution to "Pandas - Combine two Columns with a comma between them"

Hello mikec513<\/p>\n\n

Here is a method that combine two columns with a separator.<\/strong><\/p>\n

def<\/span> <\/span>combine_two_columns<\/span>(<\/span>dataframe<\/span>,<\/span> <\/span>column_name_1<\/span>,<\/span> <\/span>column_name_2<\/span>,<\/span> <\/span>separator<\/span>=&<\/span>quot<\/span>;,<\/span>&<\/span>quot<\/span>;)<\/span>:<\/span>\n    <\/span>column_names<\/span> <\/span>=<\/span> <\/span>dataframe<\/span>.<\/span>columns<\/span>.<\/span>values<\/span>.<\/span>tolist<\/span>()<\/span>\n    <\/span>list_of_all_columns<\/span> <\/span>=<\/span> <\/span>[]<\/span>\n    <\/span>choosen_column_1<\/span> <\/span>=<\/span> <\/span>[]<\/span>\n    <\/span>choosen_column_2<\/span> <\/span>=<\/span> <\/span>[]<\/span>\n    <\/span>choosen_column_1_and_choosen_column_2_merged<\/span> <\/span>=<\/span> <\/span>[]<\/span>\n    <\/span>raw_data<\/span> <\/span>=<\/span> <\/span>dict<\/span>()<\/span>\n\n    <\/span>for<\/span> <\/span>name<\/span> <\/span>in<\/span> <\/span>column_names:<\/span>\n        <\/span>list_of_all_columns<\/span>.<\/span>append<\/span>([<\/span>name<\/span>]<\/span> <\/span>+<\/span> <\/span>dataframe<\/span>[<\/span>name<\/span>]<\/span>.<\/span>values<\/span>.<\/span>tolist<\/span>())<\/span>\n\n    <\/span>for<\/span> <\/span>column<\/span> <\/span>in<\/span> <\/span>list_of_all_columns:<\/span>\n        <\/span>if<\/span> <\/span>(<\/span>column<\/span>[<\/span>0<\/span>]<\/span> <\/span>!=<\/span> <\/span>column_name_1<\/span> <\/span>and<\/span> <\/span>column<\/span>[<\/span>0<\/span>]<\/span> <\/span>!=<\/span> <\/span>column_name_2<\/span>)<\/span>:<\/span>\n            <\/span>continue<\/span>\n        <\/span>elif<\/span> <\/span>(<\/span>column<\/span>[<\/span>0<\/span>]<\/span> <\/span>==<\/span> <\/span>column_name_1<\/span>)<\/span>:<\/span>\n            <\/span>choosen_column_1<\/span> <\/span>=<\/span> <\/span>column<\/span>\n        <\/span>else:<\/span>\n            <\/span>choosen_column_2<\/span> <\/span>=<\/span> <\/span>column<\/span>\n\n    <\/span>choosen_column_1_and_choosen_column_2_merged<\/span>.<\/span>append<\/span>(<\/span>choosen_column_1<\/span>[<\/span>0<\/span>]<\/span> <\/span>+<\/span> <\/span>&<\/span>quot<\/span>;<\/span>_and_&quot<\/span>;<\/span> <\/span>+<\/span> <\/span>choosen_column_2<\/span>[<\/span>0<\/span>])<\/span>\n    <\/span>for<\/span> <\/span>value_from_column_1<\/span>,<\/span> <\/span>value_from_column_2<\/span> <\/span>in<\/span> <\/span>zip<\/span>(<\/span>choosen_column_1<\/span>[<\/span>1<\/span>:<\/span>],<\/span> <\/span>choosen_column_2<\/span>[<\/span>1<\/span>:<\/span>])<\/span>:<\/span>\n        <\/span>if<\/span> <\/span>(<\/span>value_from_column_1<\/span> <\/span>!=<\/span> <\/span>None<\/span> <\/span>and<\/span> <\/span>value_from_column_2<\/span> <\/span>!=<\/span> <\/span>None<\/span>)<\/span>:<\/span>\n            <\/span>choosen_column_1_and_choosen_column_2_merged<\/span>.<\/span>append<\/span>(<\/span>value_from_column_1<\/span> <\/span>+<\/span> <\/span>separator<\/span> <\/span>+<\/span> <\/span>value_from_column_2<\/span>)<\/span>\n        <\/span>elif<\/span> <\/span>(<\/span>value_from_column_1<\/span> <\/span>!=<\/span> <\/span>None<\/span> <\/span>and<\/span> <\/span>value_from_column_2<\/span> <\/span>==<\/span> <\/span>None<\/span>)<\/span>:<\/span>\n            <\/span>choosen_column_1_and_choosen_column_2_merged<\/span>.<\/span>append<\/span>(<\/span>value_from_column_1<\/span> <\/span>+<\/span> <\/span>separator<\/span>)<\/span>\n        <\/span>elif<\/span> <\/span>(<\/span>value_from_column_1<\/span> <\/span>==<\/span> <\/span>None<\/span> <\/span>and<\/span> <\/span>value_from_column_2<\/span> <\/span>!=<\/span> <\/span>None<\/span>)<\/span>:<\/span>\n            <\/span>choosen_column_1_and_choosen_column_2_merged<\/span>.<\/span>append<\/span>(<\/span>separator<\/span> <\/span>+<\/span> <\/span>value_from_column_2<\/span>)<\/span>\n    <\/span>list_of_all_columns<\/span>.<\/span>append<\/span>(<\/span>choosen_column_1_and_choosen_column_2_merged<\/span>)<\/span>\n\n    <\/span>for<\/span> <\/span>column<\/span> <\/span>in<\/span> <\/span>list_of_all_columns:<\/span>\n        <\/span>if<\/span> <\/span>(<\/span>column<\/span>[<\/span>0<\/span>]<\/span> <\/span>!=<\/span> <\/span>column_name_1<\/span> <\/span>and<\/span> <\/span>column<\/span>[<\/span>0<\/span>]<\/span> <\/span>!=<\/span> <\/span>column_name_2<\/span>)<\/span>:<\/span>\n            <\/span>raw_data<\/span>[<\/span>column<\/span>[<\/span>0<\/span>]]<\/span> <\/span>=<\/span> <\/span>column<\/span>[<\/span>1<\/span>:<\/span>]<\/span>\n\n    <\/span>return<\/span> <\/span>pd<\/span>.<\/span>DataFrame<\/span>(<\/span>raw_data<\/span>)<\/span>\n<\/pre><\/div>\n

The following three lines give the result you want<\/strong><\/p>\n

new_df<\/span> <\/span>=<\/span> <\/span>combine_two_columns<\/span>(<\/span>df<\/span>,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>orgSourcedIds&quot<\/span>;,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>employeeType&quot<\/span>;)<\/span>\ncolumn_names<\/span> <\/span>=<\/span> <\/span>[<\/span>&<\/span>quot<\/span>;<\/span>enabledUser&quot<\/span>;,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>sourcedId&quot<\/span>;,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>orgSourcedIds_and_employeeType&quot<\/span>;,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>role&quot<\/span>;,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>familyName&quot<\/span>;,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>givenName&quot<\/span>;,<\/span> <\/span>&<\/span>quot<\/span>;<\/span>username&quot<\/span>;]<\/span>\nnew_df<\/span> <\/span>=<\/span> <\/span>new_df<\/span>[<\/span>column_names<\/span>]<\/span>\n<\/pre><\/div>\n

Best,
\nJana<\/p>\n

Hello mikec513 **Here is a method that combine two columns with a separator.** .** ``` def combine_two_columns(dataframe, column_name_1, column_name_2, separator=","): column_names = dataframe.columns.values.tolist() list_of_all_columns = [] choosen_column_1 = [] choosen_column_2 = [] choosen_column_1_and_choosen_column_2_merged = [] raw_data = dict() for name in column_names: list_of_all_columns.append([name] + dataframe[name].values.tolist()) for column in list_of_all_columns: if (column[0] != column_name_1 and column[0] != column_name_2): continue elif (column[0] == column_name_1): choosen_column_1 = column else: choosen_column_2 = column choosen_column_1_and_choosen_column_2_merged.append(choosen_column_1[0] + "_and_" + choosen_column_2[0]) for value_from_column_1, value_from_column_2 in zip(choosen_column_1[1:], choosen_column_2[1:]): if (value_from_column_1 != None and value_from_column_2 != None): choosen_column_1_and_choosen_column_2_merged.append(value_from_column_1 + separator + value_from_column_2) elif (value_from_column_1 != None and value_from_column_2 == None): choosen_column_1_and_choosen_column_2_merged.append(value_from_column_1 + separator) elif (value_from_column_1 == None and value_from_column_2 != None): choosen_column_1_and_choosen_column_2_merged.append(separator + value_from_column_2) list_of_all_columns.append(choosen_column_1_and_choosen_column_2_merged) for column in list_of_all_columns: if (column[0] != column_name_1 and column[0] != column_name_2): raw_data[column[0]] = column[1:] return pd.DataFrame(raw_data) **) ``` **The following three lines give the result you want** ** ``` new_df = combine_two_columns(df, "orgSourcedIds", "employeeType") column_names = ["enabledUser", "sourcedId", "orgSourcedIds_and_employeeType", "role", "familyName", "givenName", "username"] new_df = new_df[column_names] ] ``` Best,
Jana
Hello mikec513 **Here is a method that combine two columns with a separator.** ``` .** def combine_two_columns(dataframe, column_name_1, column_name_2, separator=","): column_names = dataframe.columns.values.tolist() list_of_all_columns = [] choosen_column_1 = [] choosen_column_2 = [] choosen_column_1_and_choosen_column_2_merged = [] raw_data = dict() for name in column_names: list_of_all_columns.append([name] + dataframe[name].values.tolist()) for column in list_of_all_columns: if (column[0] != column_name_1 and column[0] != column_name_2): continue elif (column[0] == column_name_1): choosen_column_1 = column else: choosen_column_2 = column choosen_column_1_and_choosen_column_2_merged.append(choosen_column_1[0] + "_and_" + choosen_column_2[0]) for value_from_column_1, value_from_column_2 in zip(choosen_column_1[1:], choosen_column_2[1:]): if (value_from_column_1 != None and value_from_column_2 != None): choosen_column_1_and_choosen_column_2_merged.append(value_from_column_1 + separator + value_from_column_2) elif (value_from_column_1 != None and value_from_column_2 == None): choosen_column_1_and_choosen_column_2_merged.append(value_from_column_1 + separator) elif (value_from_column_1 == None and value_from_column_2 != None): choosen_column_1_and_choosen_column_2_merged.append(separator + value_from_column_2) list_of_all_columns.append(choosen_column_1_and_choosen_column_2_merged) for column in list_of_all_columns: if (column[0] != column_name_1 and column[0] != column_name_2): raw_data[column[0]] = column[1:] return pd.DataFrame(raw_data) ``` **) **The following three lines give the result you want** ``` ** new_df = combine_two_columns(df, "orgSourcedIds", "employeeType") column_names = ["enabledUser", "sourcedId", "orgSourcedIds_and_employeeType", "role", "familyName", "givenName", "username"] new_df = new_df[column_names] ``` ] Best,
Jana
Hello mikec513 **Here is a method that combine two columns with a separator.** def combine_two_columns(dataframe, column_name_1, column_name_2, separator=","): column_names = dataframe.columns.values.tolist() list_of_all_columns = [] choosen_column_1 = [] choosen_column_2 = [] choosen_column_1_and_choosen_column_2_merged = [] raw_data = dict() () for name in column_names: list_of_all_columns.append([name] + dataframe[name].values.tolist()) for column in list_of_all_columns: if (column[0] != column_name_1 and column[0] != column_name_2): continue elif (column[0] == column_name_1): choosen_column_1 = column else: choosen_column_2 = column choosen_column_1_and_choosen_column_2_merged.append(choosen_column_1[0] + "_and_" + choosen_column_2[0]) for value_from_column_1, value_from_column_2 in zip(choosen_column_1[1:], choosen_column_2[1:]): if (value_from_column_1 != None and value_from_column_2 != None): choosen_column_1_and_choosen_column_2_merged.append(str(value_from_column_1 + ) + str(separator + ) + str(value_from_column_2) )) elif (value_from_column_1 != None and value_from_column_2 == None): choosen_column_1_and_choosen_column_2_merged.append(str(value_from_column_1 + ) + str(separator) )) elif (value_from_column_1 == None and value_from_column_2 != None): choosen_column_1_and_choosen_column_2_merged.append(str(separator + ) + str(value_from_column_2) )) list_of_all_columns.append(choosen_column_1_and_choosen_column_2_merged) ) for column in list_of_all_columns: if (column[0] != column_name_1 and column[0] != column_name_2): raw_data[column[0]] = column[1:] return pd.DataFrame(raw_data) **The following three lines give the result you want** new_df = combine_two_columns(df, "orgSourcedIds", "employeeType") column_names = ["enabledUser", "sourcedId", "orgSourcedIds_and_employeeType", "role", "familyName", "givenName", "username"] new_df = new_df[column_names] Best,
Jana

User: Jana

Question: Pandas - Combine two Columns with a comma between them

Back to question