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 then I could not, 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, created the term columns and everything, the problem is in the next, I tried to create a function so that when the value of the Term Column is equal to 5 days, it creates a discount column with the information of 10%, I tried:
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 as follows:
KeyError: ' 5 dias '
The space between the "in 5 days" is on purpose, the column is like this, something that I will try to solve in the next one, but I wanted to know what I'm missing in the function.
If anyone can help, I'm very grateful!
Hugs,