简体   繁体   中英

how to split rows based on separator in csv file in pandas?

I am trying to split row based on separator but I am not able to get right result. Can someone help me how to do this?

Example:

df1:

rule_id    priority_order    comb_fld_order   
R162       2.3               1
R162       2.3.1             1
R162       2.6               2
R162       2.6.1             2
R162       3.0.4             3.2,3.1,3

Expected Result:

df2:

rule_id    priority_order    comb_fld_order   comb_fld_order_1
R162       2.3               1                 1
R162       2.3.1             1                 1
R162       2.6               2                 2
R162       2.6.1             2                 2
R162       3.0.4             3.2,3.1,3         3.2
R162       3.0.4             3.2,3.1,3         3.1
R162       3.0.4             3.2,3.1,3         3

I am writing this code for generating df1:

conn = redshift_conn()
cur = conn.cursor()
query = '''select rule_id,priority_order,comb_fld_order from medaff.imedical_business_rules_metadata  WHERE RULE_ID='R162' and comb_fld_order is not null order by priority_order'''
cur.execute(query)
res = cur.fetchall()
column_names = [i[0] for i in cur.description]

df = pd.DataFrame(res, columns=column_names)

I faced the same issue right now. I've solved it with pandas.explode()

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM