Function to create Column

Asked

Viewed 466 times

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"]

1 answer

1


Its function cria_colunas receives a string. Do df[string_campanha] causes a column to be searched in the df with the name string_campanha. Since there is no such column, it generates the error KeyError: ' 5 dias '. What you want to do is check if there is a string in the 'Uploads' column and generate a 'Discount' column from there. When we do

df['Desconto'] = df['Envios'].apply(cria_colunas)

We are applying the creat_columns function to each row of the 'Uploads' column and placing the answer in the respective row of the 'Discount' column, created now.

The correct function would be:

def cria_colunas(linha_envios):
    if ' 5 dias ' in linha_envios:
        return '10%'
    else:
        return ''
  • Thanks @Alexciuffa

Browser other questions tagged

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