import pandas as pd
import numpy as np
import matplotlib as plt
df = pd.read_csv('dito_julho.csv')
df.head()
campanha valor
1 Prospect | 5 dias | Com crédito 2
2 Prospect | 5 dias | Com crédito 5
3 Prospect | 5 dias | Com crédito 7
So I try to create a new column with the second information of each row in column 1, ie, I want to get the "5 Days"
df_teste = df['Segmento'].apply(lambda x: x.split("|")[1])
However, it gives the error below:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-180-57ecc844181a> in <module>()
----> 1 df_teste = df['Segmento'].apply(lambda x: x.split("|")[1])
c:\users\iuri\appdata\local\programs\python\python36-32\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
3192 else:
3193 values = self.astype(object).values
-> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype)
3195
3196 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-180-57ecc844181a> in <lambda>(x)
----> 1 df_teste = df['Segmento'].apply(lambda x: x.split("|")[1])
IndexError: list index out of range
If I try to do with the first field, which is Prospect, it works:
df_teste = df['Segmento'].apply(lambda x: x.split("|")[1])
df_teste.head()
>>>>>>>
0 Prospect
1 Prospect
2 Prospect
3 Prospect
4 Prospect
Does anyone have any tips on why I can not get this information?
If I test, creating something like this:
df_teste = df['Segmento'].apply(lambda x: x.split("|"))
df_teste.head()
>>>>>
0 [Prospect , 5 dias , Com crédito]
1 [Prospect , 20 dias , Com crédito]
2 [Prospect , 40 dias , Com crédito]
3 [Prospect , 75 dias , Com crédito]
4 [Prospect , 5 dias , Sem crédito]
It's clear that you could pick up the information, 1, the days, but that does not happen.
Could anyone help me?