windyvation
windyvation

Reputation: 561

Pandas to create new rows from a cell that has multiple values in python

I've seen some similar problems on stack, but not exactly like this. I appreciate if you could share a way to turn df into df2.

df = pd.DataFrame({'zips': ['11229 11226 11225', '90291 90293 90292'],
                  'lat': [40.6943, 34.1139],
                  'lng': [-73.9249, -118.4068]})


df2 = pd.DataFrame({'zips': [11229, 11226, 11225, 90291, 90293, 90292],
                  'lat': [40.6943, 40.6943, 40.6943, 34.1139, 34.1139, 34.1139],
                  'lng': [-73.9249, -73.9249, -73.9249, -118.4068, -118.4068, -118.4068]})```

Thanks!

Upvotes: 0

Views: 679

Answers (1)

ThePyGuy
ThePyGuy

Reputation: 18426

You can first split the values in zips column on space which will give a list, then just use explode

df['zips'] = df['zips'].str.split()
df.explode('zips')

OUTPUT:

    zips      lat       lng
0  11229  40.6943  -73.9249
1  11226  40.6943  -73.9249
2  11225  40.6943  -73.9249
3  90291  34.1139 -118.4068
4  90293  34.1139 -118.4068
5  90292  34.1139 -118.4068

You can further change the data type of zips column to integer if you wish to by type casting

df['zips'] = df['zips'].astype(int)

Upvotes: 2

Related Questions