3

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.

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

perfect as always! Thank you! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.