I have a dataset looks like this:
I want to split the second column and then append the values like this using python or pandas:
How could I change it? Thanks
CodePudding user response:
The pandas DataFrame has explode
method that does exactly what you want. See explode() documentation. It works with list-like object, so if the column you want to explode is of type string, then you need to split it into list. See str.split() documentation. Additionally you can remove any white spaces with Pandas map function.
Full code example:
import pandas as pd
df = pd.DataFrame({
"x": [1,2,3,4],
"y": ["a, b, c, d", "e, f, g", "h, i", "j, k, l, m, n"]
})
# Convert string with commas into list of string and strip spaces
df['y'] = df['y'].str.split(',').map(lambda elements: [e.strip() for e in elements])
# Explode lists in the column 'y' into separate values
df.explode('y')
Output:
x y
0 1 a
0 1 b
0 1 c
0 1 d
1 2 e
1 2 f
1 2 g
2 3 h
2 3 i
3 4 j
3 4 k
3 4 l
3 4 m
3 4 n
CodePudding user response:
You can make use of explode
df = pd.DataFrame({'A': [1, 2, 3, 4],
'B': ['ab,s', 'a,s,d,f', 'rk,lw', 'get,me']})
# Split by each comma
df.B = df.B.str.split(',')
df.explode('B')