Fahmieyz
Fahmieyz

Reputation: 255

Split row that have multiple value in a cell and append back to the data list

enter image description here

I have this dataframe, in which some values in Postcode may have more than 1 postcode. What I try to do is to split the row that have multiple postcode and then append back into the dataframe.

I already get the list of index that have multiple postcode using following code;

index_list = df_selangor[df_selangor['Postcode'].str.contains(' ')].index

This allow me to create new dataframe and then split the value in Postcode like this;

df_selangor_split = df_selangor.copy()
df_selangor_split = df_selangor_split[df_selangor_split.index.isin(index_list)]
df_selangor_split['Postcode'] = df_selangor_split['Postcode'].str.split()

enter image description here

However, I stuck after this step. I not sure how to split it again so that the Area is copied and have only 1 postcode.

Upvotes: 1

Views: 182

Answers (1)

Ajay Shah
Ajay Shah

Reputation: 414

pd.concat([pd.Series(row['Area'], row['Postcode'].split(','))
         for _, row in dfx.iterrows()])

Basically, we are iterating each row and splitting the postcode column for each area and then concatenating it.

Upvotes: 2

Related Questions