Bruno Mello :
Suppose I have the following dataframe:
df = pd.DataFrame({'col1': ['abc', 'defg', 'hwer', 'qwerty'], 'col2': ['123', '456', '890', '90'],
'col3': ['knlk', 'knl', 'op', 'tui']})
And I want to join the strings from each row by a specific character, I'm doing like this:
df['col4'] = df['col1'] + '_' + df['col2'] + '_' + df['col3']
But I have to keep repeating the '_'
, is there a way to do something like:
df['col4'] = '_'.join([df['col1'], df['col2'], df['col3']])
kederrac :
you can use pandas.DataFrame.stack:
df['col4'] = df.stack().groupby(level=0).apply('_'.join)
df
output:
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=383208&siteId=1