Below is the data frame I have:
ID Char Location
1 a IN
2 b,c,d US
3 e,g IN
4 ,,, CA
Below is the data frame I desire:
ID Char Location
1 a IN
2 b US
2 c US
2 d US
3 e IN
3 g IN
4 CA
How can I transform this data frame?
Try this:
df = pd.DataFrame({'ID': [1,2,3,4], 'Char': ['a', 'b,c,d', 'e,g', ',,,'], 'Location': ['IN', 'US', 'IN', 'CA']})
# copy the original order of columns
org_cols = df.columns.copy()
# explode Char column
df = df.drop(columns='Char').join(df['Char'].str.split(',').apply(lambda x: list(set(x))).explode())
# reorder columns
df = df[org_cols]
df
ID Char Location
0 1 a IN
1 2 c US
1 2 b US
1 2 d US
2 3 e IN
2 3 g IN
3 4 CA
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.