复制特定行并分隔 str 列值 pandas
Replicate specific rows and separate str column values pandas
我有以下数据框
index city pop
1 London 1000
2 Buenos Aires 2054
3 New York 34544
4 Paris-Madrid 34343
5 Barcelona-Lagos 64544
我希望做的事情:
index city pop
1 London 1000
2 Buenos Aires 2054
3 New York 34544
4 Paris 34343
5 Madrid 34343
6 Barcelona 64544
7 Lagos 64544
我该怎么做?
split
+ explode
df=df.assign(city=df.city.str.split('-')).explode('city').reset_index(drop=True)
index city pop
0 1 London 1000
1 2 BuenosAires 2054
2 3 NewYork 34544
3 4 Paris 34343
4 4 Madrid 34343
5 5 Barcelona 64544
6 5 Lagos 64544
我有以下数据框
index city pop
1 London 1000
2 Buenos Aires 2054
3 New York 34544
4 Paris-Madrid 34343
5 Barcelona-Lagos 64544
我希望做的事情:
index city pop
1 London 1000
2 Buenos Aires 2054
3 New York 34544
4 Paris 34343
5 Madrid 34343
6 Barcelona 64544
7 Lagos 64544
我该怎么做?
split
+ explode
df=df.assign(city=df.city.str.split('-')).explode('city').reset_index(drop=True)
index city pop
0 1 London 1000
1 2 BuenosAires 2054
2 3 NewYork 34544
3 4 Paris 34343
4 4 Madrid 34343
5 5 Barcelona 64544
6 5 Lagos 64544