Split a column into two from a parameter - Python

Asked

Viewed 2,310 times

-1

I researched a solution but I can’t locate facing a data frame, just input lines for example.

I HAVE A DATAFRAME WITH THE FOLLOWING FIELDS AND LINES:

data = {'País': ['Bélgica', 'Índia', 'Brasil'],
'Capital': ['Bruxelas', 'Nova Delhi', 'Brasília'],
'População': ['123/A465', '456/A789', '987/A654']}

df_DIV = pd.DataFrame(data, columns=['País','Capital','População'])

CURRENT EXIT:

inserir a descrição da imagem aqui

I need to divide the Population column into two where the parameter would be "/" and there is no pattern in the amount of character before ' ' so I can divide with df['New Field'] = df['Population']. str[:5] for example, so I would need it to stay that way:

inserir a descrição da imagem aqui

BUT THAT’S JUST AN EXAMPLE OF WHAT I NEED TO DO IN A 300,000-LINE DF.

2 answers

0


Good afternoon!

Below is an example of the split to create new columns in df.

import pandas as pd 

# Lendo o arquivo CSV e carregando para "data"
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") 

# Imprimindo Original no Console
print(data)

# Descartando colunas com valor nulo para evitar erros
data.dropna(inplace = True)

# Nova Data com Split das coluna "Name" separado por espaços
new = data["Name"].str.split(" ", n = 1, expand = True)

# Criando a Nova Coluna "First Name" com o new[0]
data["First Name"]= new[0] 

# Criando a Nova Coluna "Last Name" com o new[1]
data["Last Name"]= new[1] 

# Retirando a antiga coluna "Name" 
data.drop(columns =["Name"], inplace = True) 

# Imprimindo Alteração no Console
print(data)

It’s well explained, see if it helps.

  • Thanks Cleverson, I also found a split method commented below.

-1

Localized solution:

df['Campo1'], df['Campo2'] = df['Population'].str.split('/', 1). str

del df[Population]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.