For example I have two rows:
ID,dep_id
EMP1,1,2,3
EMP2,4,5,6
I want the output as:
ID,dep_id
EMP1,1
EMP1,2
EMP1,3
EMP2,4
EMP2,5
EMP2,6
You can use pandas
for this:
import pandas as pd
import io
data = '''ID,dep_id
EMP1,"1,2,3"
EMP2,"4,5,6"'''
df = pd.read_csv(io.StringIO(data)) # insert your csv here
df['dep_id'] = ('['+df['dep_id']+ ']').apply(pd.eval) # turn string to list
df.explode('dep_id')df.explode('dep_id').reset_index(drop=True)
Output:
ID | dep_id | |
---|---|---|
0 | EMP1 | 1 |
1 | EMP1 | 2 |
2 | EMP1 | 3 |
3 | EMP2 | 4 |
4 | EMP2 | 5 |
5 | EMP2 | 6 |
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.