Issue
I have a dataframe with a column of lists which can be created with:
import pandas as pd
lists={1:[[1,2,12,6,'ABC']],2:[[1000,4,'z','a']]}
#create test dataframe
df=pd.DataFrame.from_dict(lists,orient='index')
df=df.rename(columns={0:'lists'})
The dataframe df
looks like:
lists
1 [1, 2, 12, 6, ABC]
2 [1000, 4, z, a]
I need to create a new column called 'liststring
' which takes every element of each list in lists
and creates a string with each element separated by commas. The elements of each list can be int
, float
, or string
. So the result would be:
lists liststring
1 [1, 2, 12, 6, ABC] 1,2,12,6,ABC
2 [1000, 4, z, a] 1000,4,z,a
I have tried various things, including from Converting a Panda DF List into a string:
df['liststring']=df.lists.apply(lambda x: ', '.join(str(x)))
but unfortunately the result takes every character and seperates by comma:
lists liststring
1 [1, 2, 12, 6, ABC] [, 1, ,, , 2, ,, , 1, 2, ,, , 6, ,, , ', A...
2 [1000, 4, z, a] [, 1, 0, 0, 0, ,, , 4, ,, , ', z, ', ,, , '...
Thanks in advance for the help!
Solution
List Comprehension
If performance is important, I strongly recommend this solution and I can explain why.
df['liststring'] = [','.join(map(str, l)) for l in df['lists']]
df
lists liststring
0 [1, 2, 12, 6, ABC] 1,2,12,6,ABC
1 [1000, 4, z, a] 1000,4,z,a
You can extend this to more complicated use cases using a function.
def try_join(l):
try:
return ','.join(map(str, l))
except TypeError:
return np.nan
df['liststring'] = [try_join(l) for l in df['lists']]
Series.apply
/Series.agg
with ','.join
You need to convert your list items to strings first, that's where the map
comes in handy.
df['liststring'] = df['lists'].apply(lambda x: ','.join(map(str, x)))
Or,
df['liststring'] = df['lists'].agg(lambda x: ','.join(map(str, x)))
<!- >
df
lists liststring
0 [1, 2, 12, 6, ABC] 1,2,12,6,ABC
1 [1000, 4, z, a] 1000,4,z,a
pd.DataFrame
constructor with DataFrame.agg
A non-loopy/non-lambda solution.
df['liststring'] = (pd.DataFrame(df.lists.tolist())
.fillna('')
.astype(str)
.agg(','.join, 1)
.str.strip(','))
df
lists liststring
0 [1, 2, 12, 6, ABC] 1,2,12,6,ABC
1 [1000, 4, z, a] 1000,4,z,a
Answered By - cs95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.