1
Hello,
I’m trying to create a column using a function, but it’s giving an error, which should be silly to fix, and even so I couldn’t, come on:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv("Planilha_Final.csv")
# Criando Coluna com o Nome da Regua, Prazo e se tem ou não Crédito
df = df[df['Segmento'].str.contains('|')]
def cria_acao(string_nome):
try:
return string_nome.split("|")[0]
except:
return ''
def cria_prazo(string_nome):
try:
return string_nome.split("|")[1]
except:
return ''
def cria_credito(string_nome):
try:
return string_nome.split("|")[2]
except:
return ''
df['Régua'] = df['Segmento'].apply(cria_acao)
df['Prazo'] = df['Segmento'].apply(cria_prazo)
df['Crédito'] = df['Segmento'].apply(cria_credito)
This function above worked well, created the columns of term and everything else, the problem is next, I tried to create a function so that when the value of Column Term is equal to 5 days, it creates a column discount with the information of 10%, I tried so:
def cria_colunas(string_campanha):
if df[string_campanha].str.contains(' 5 dias ') == True:
return '10%'
else:
return ''
df['Desconto'] = df['Envios'].apply(cria_colunas)
And the error that appears is this:
KeyError: ' 5 dias '
The gap between the ' ' ' in 5 days is on purpose, the column is like this, which I will try to solve in a next, but I wanted to know what I am missing in the function.
If anyone can help, I’m very grateful!
Hugs,
Try normalizing the strings to make the comparison. If you only have "5 days" in the column try to use . strip() .. and up to "5 days" in df["Uploads"]
– Marlysson