0
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 of 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 the 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 hint as to why I can’t get this information?
If I do a 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 is clear that could take the information, 1, the days, but this does not occur.
Could someone help me?
Are you trying to use the right column? In your example the column calls "campaign". And this
apply
works properly.– Lorran Sutter
In both examples you put
[1]
, in the second would not be 0?– Woss
Are you sure all the lines have
|
? Just one of your csv won’t have to break the code.– Begnini
You killed the charade @Begnini some lines don’t have that, I managed to solve by listing only those that have the "|"
– Iuri Moura