Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a column in pandas twice to multiple columns

I have a column "Nome_propriedade" with complete addresses, such as establishment name, streets, neighborhood, city and state

It always ends with the name of the city and state. With this pattern:

Nome_propriedade
"Rod. BR 386, bairro Olarias/Conventos, Lajeado/RS"
"Fazenda da Várzea - zona rural, Serro/MG"
"Cidade do Rock - Jacarepaguá, Rio de Janeiro/RJ"
"Área de extração de carnaúba - Povoado Areal, zona rural, Santa Cruz do Piauí/PI"
"Pastelaria - Av. Vicente de Carvalho, 995, Loja Q, Vila da Penha, Rio de Janeiro/RJ"

I want to create two new columns, "city" and "state", and fill them with the last values found in column "Nome_propriedade". I also want to stip those away from Nome_propiedade.

                           Nome_propriedade                 City State
      Rod. BR 386, bairro Olarias/Conventos              Lajeado    RS
             Fazenda da Várzea - zona rural                Serro    MG
            Cidade do Rock - Jacarepaguá...       Rio de Janeiro    RJ
Área de extração de carnaúba - Povoado A...  Santa Cruz do Piauí    PI
Pastelaria - Av. Vicente de Carvalho, 99...       Rio de Janeiro    RJ

Please anyone know how I can create these two columns?

I can not do a general split because I just want to separate the city and state information. Other information may remain unchanged.

like image 706
Reinaldo Chaves Avatar asked Nov 25 '25 13:11

Reinaldo Chaves


2 Answers

What do you think about:

import pandas as pd
propiedades = ["Rod. BR 386, bairro Olarias/Conventos, Lajeado/RS",
               "Fazenda da Várzea - zona rural, Serro/MG",
               "Cidade do Rock - Jacarepaguá, Rio de Janeiro/RJ",
               "Área de extração de carnaúba - Povoado Areal, zona rural, Santa Cruz do Piauí/PI",
               "Pastelaria - Av. Vicente de Carvalho, 995, Loja Q, Vila da Penha, Rio de Janeiro/RJ"]
df = pd.DataFrame({"Nome_propriedade":propiedades})

df[["City", "State"]] = df["Nome_propriedade"].apply(lambda x :x.split(",")[-1]).str.split("/",
                                                                                           expand=True)

UPDATE If you then want to delete these infos from Nome_propriedade you can add this line

df["Nome_propriedade"] = df["Nome_propriedade"].apply(lambda x :",".join(x.split(",")[:-1]))
like image 64
rpanai Avatar answered Nov 28 '25 01:11

rpanai


You need to split the string in the column by ,, takw the last element in the list and split it by /. That list is your two columns.

pd.DataFrame(list(df['Nome_propriedade'].str.split(',').apply(lambda x: x[-1]).str.split('/')), columns=['city', 'state'])

Output:

                   city    state
0               Lajeado    RS
1                 Serro    MG
2        Rio de Janeiro    RJ
3   Santa Cruz do Piauí    PI
4        Rio de Janeiro    RJ
like image 34
harvpan Avatar answered Nov 28 '25 02:11

harvpan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!