MaMo
MaMo

Reputation: 585

Python: transform each array within a column cell into single string

My data frame looks like this:

df = pd.DataFrame({'col1': [1, 2, 3 ,4 , 5, 6], 'txt': [[2354],[103, 132, 2457],[132, 1476, 6587],[103, 2457],[103, 1476, 2354], np.nan]})

   col1                txt
0     1             [2354]
1     2   [103, 132, 2457]
2     3  [132, 1476, 6587]
3     4        [103, 2457]
4     5  [103, 1476, 2354]
5     6                NaN

Column 'txt' contains an array or NaN in each cell.

Now I would like to keep the dataframe structure as it is but the arrays should be a string containing all elements seperated by comma.

Required output (with string instead of array):

   col1                txt
0     1               2354
1     2     103, 132, 2457
2     3    132, 1476, 6587
3     4          103, 2457
4     5    103, 1476, 2354
5     6                NaN

Solutions that I found did not work for a column.

Thank you.

Upvotes: 3

Views: 1220

Answers (1)

jezrael
jezrael

Reputation: 863511

Use list comprehension only in filtered rows - if no missing values, but also is necessary convert all numeric columns to strings - by map or in generator cast to string:

mask = df['txt'].notnull()
df.loc[mask, 'txt'] = [', '.join(map(str, x)) for x in df.loc[mask, 'txt']]
#alternative solution
#df.loc[mask, 'txt'] = df.loc[mask, 'txt'].apply(lambda x: ', '.join(map(str, x)))
#another solution
#df.loc[mask, 'txt'] = [', '.join(str(i) for i in x) for x in df.loc[mask, 'txt']]

print (df)
   col1              txt
0     1             2354
1     2   103, 132, 2457
2     3  132, 1476, 6587
3     4        103, 2457
4     5  103, 1476, 2354
5     6              NaN

Upvotes: 3

Related Questions