Issue
i have a dataframe with two columns of array.
Column 1
0 [254.0, 254.0, 254.0, 254.0, 254.0]
1 [254.0, 254.0, 254.0, 254.0, 254.0]
2 [254.0, 254.0, 254.0, 254.0, 254.0]
3 [254.0, 254.0, 254.0, 254.0, 254.0]
4 [254.0, 254.0, 254.0, 254.0, 254.0]
Column 2
0 [3.0, 4.0, 7.0, 1.0, 5.0]
1 [3.0, 4.0, 7.0, 1.0, 5.0]
2 [3.0, 4.0, 7.0, 1.0, 5.0]
3 [3.0, 4.0, 7.0, 1.0, 5.0]
4 [3.0, 4.0, 7.0, 1.0, 5.0]
I am trying to create a third column where each cell looks like a dictionary of arrays. It would look like the following:
Column 3
0 {0:[254.0,3], 1:[254.0,4], 2:[254.0,7], 3:[254.0,1], 4:[254.0,5]}
1 {0:[254.0,3], 1:[254.0,4], 2:[254.0,7], 3:[254.0,1], 4:[254.0,5]}
2 {0:[254.0,3], 1:[254.0,4], 2:[254.0,7], 3:[254.0,1], 4:[254.0,5]}
3 {0:[254.0,3], 1:[254.0,4], 2:[254.0,7], 3:[254.0,1], 4:[254.0,5]}
4 {0:[254.0,3], 1:[254.0,4], 2:[254.0,7], 3:[254.0,1], 4:[254.0,5]}
how do i do this ?
Solution
How about:
df['Column 3'] = df.apply(lambda x: dict(enumerate(zip(x['Column 1'], x['Column 2']))), axis=1)
Answered By - Quang Hoang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.