I have a similarly formatted input to what provided below, that I'm trying to convert into a python DataFrame
my_input = [
{'comp_id': '111', 'name': 'A-Name', 'brnch_id': ['BR-AA1', 'BR-AA2']},
{'comp_id': '222', 'name': 'B-Name', 'brnch_id': ['BR-BB1', 'BR-BB2', 'BR-BB3']},
{'comp_id': '333', 'name': 'C-Name', 'brnch_id': None}
]
The desired output should be:
comp_id name brnch_id
------- ------ --------
111 A-Name BR-AA1
111 A-Name BR-AA2
222 B-Name BR-BB1
222 B-Name BR-BB2
222 B-Name BR-BB3
333 C-Name
Any suggestions?
The explode method seems useful, but I should have been more diligent with my question and suggest that the data input could have more than one column that requires expanding, so here is a the revised challenge:
[
{'comp_id': '111', 'name': 'A-Name', 'brnch_id': ['BR-1111', 'BR-1112'], 'brnch_name': ['AA1','AA2']},
{'comp_id': '222', 'name': 'B-Name', 'brnch_id': ['BR-2221', 'BR-2222', 'BR-2223'], 'brnch_name': ['BB1','BB2','BB3']},
{'comp_id': '333', 'name': 'C-Name', 'brnch_id': None, 'brnch_name': None}
]
With a desired output as follows:
comp_id name brnch_id brnch_name
------- ------ -------- ----------
111 A-Name BR-1111 AAA1
111 A-Name BR-1112 AAA2
222 B-Name BR-2221 BBB1
222 B-Name BR-2222 BBB2
222 B-Name BR-2223 BBB3
333 C-Name
the expectation is that each comp_id should have a name and if branch exists, it should have a id (brnch_id) and brnch_name
Adding the second .explode is not producing the desired output.
comp_id name brnch_id brnch_name
0 111 A-Name BR-1111 AA1
1 111 A-Name BR-1111 AA2
2 111 A-Name BR-1112 AA1
3 111 A-Name BR-1112 AA2
4 222 B-Name BR-2221 BB1
5 222 B-Name BR-2221 BB2
6 222 B-Name BR-2221 BB3
7 222 B-Name BR-2222 BB1
8 222 B-Name BR-2222 BB2
9 222 B-Name BR-2222 BB3
10 222 B-Name BR-2223 BB1
11 222 B-Name BR-2223 BB2
12 222 B-Name BR-2223 BB3
13 333 C-Name None None
Brnch_name are being exploded for each brnch_id. so brnch_id = BR-1111 shows as having two brnch_names: AA1 & AA2.