Issue
I want to append values of a column in df1 append to values of a column in df2 and the same as for the 'b' column. the result should be:
Finaly, I have a dataframe that some of its cells have multiple lists:
df1 a b
0 [0,2,3][4,3][7,5,4,3] [3,4,5,6,7]
after printing, it is enclosed in str quotes:
'[0,2,3][4,3][7,5,4,3]' '[3,4,5,6,7]'
I want to merge the lists in a column into one:
[0,2,3,4,3,7,5,4,3]
but after running
tcs.applymap(ast.literal_eval)
I get error:
ValueError: malformed node or string: <_ast.Subscript object at 0x7fef5aa90610>
Solution
Use pd.combine
for this:
df1 = pd.DataFrame({
'a' : [[*range(5)]],
'b' : [[*range(5,10)]]
})
df2 = pd.DataFrame({
'a' : [[*range(11,14)]],
'b' : [[*range(22,25)]]
})
result = df1.combine(df2, lambda x, y: x + y)
Output result
:
a b
0 [0, 1, 2, 3, 4, 11, 12, 13] [5, 6, 7, 8, 9, 22, 23, 24]
UPDATE on the new 2nd part of the question. I really do not recommend to take that as the solution to your problem. The real problem is, HOW do you get data as a dataframe that looks like that. You really should consider working on that instead of dealing with that kind of string malformed list of lists.
Here is what you could do with regex: Input with `print(df.to_dict())
{'a': {0: '[0,2,3][4,3][7,5,4,3]'}, 'b': {0: '[3,4,5,6,7]'}}
and the code:
def change_cell(val):
pattern = r"(?<=\[)([\d\,?]+)(?=\])"
s = re.findall(pattern, val)
return [list(map(int,ls.split(','))) for ls in s]
result = df.applymap(change_cell)
print(result)
print(f"type: {type(result['a'][0])} --> cell_value: {result['a'][0]}")
Output:
a b
0 [[0, 2, 3], [4, 3], [7, 5, 4, 3]] [[3, 4, 5, 6, 7]]
type: <class 'list'> --> cell_value: [[0, 2, 3], [4, 3], [7, 5, 4, 3]]
Answered By - Rabinzel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.