3

I am trying to replace the values of 'Period' Column in this data frame:

     Year  Period        y          yhat                Contas Resultado 
0   2017       1  1.251556e+00  1.251556e+00               Devoluções   
1   2017       2  2.109900e-01  2.109899e-01               Devoluções   
2   2017       3  1.186015e+00  1.186015e+00               Devoluções   
3   2017       4  2.530208e-01  2.530208e-01               Devoluções   
4   2017       5  2.305744e-01  2.305745e-01               Devoluções   
5   2017       6  2.367768e-01  2.367768e-01               Devoluções   
6   2017       7  2.509670e-01  2.509670e-01               Devoluções   
7   2017       8  2.525350e-01  2.525350e-01               Devoluções   
8   2017       9  2.509663e-01  2.509663e-01               Devoluções   
9   2017      10  2.204747e-01  2.204747e-01               Devoluções   
10  2017      11  2.262774e-01  2.262774e-01               Devoluções   
11  2017      12  2.373548e-01  2.373548e-01               Devoluções   
12  2018       1  1.155845e+00  1.155845e+00               Devoluções   
...

Using this command:

repl_dict = {
    '01': 'M1', '02': 'M2', '03': 'M3', '04': 'M4', '05': 'M5', '06': 'M6',
    '07': 'M7', '08': 'M8', '09':'M9', '10':'M10', '11':'M11', '12':'M12'
}

results['Period'].replace(repl_dict)

However I got the following error:

TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
1
  • 2
    Your error is as stated, the data type of your Period column is an integer and you are going to a string. Commented May 22, 2018 at 14:25

5 Answers 5

5

Python 3.6+ f-strings

df.assign(Period=[f'M{i}' for i in df.Period])

    Year Period         y      yhat Contas Resultado
0   2017     M1  1.251556  1.251556       Devoluções
1   2017     M2  0.210990  0.210990       Devoluções
2   2017     M3  1.186015  1.186015       Devoluções
3   2017     M4  0.253021  0.253021       Devoluções
4   2017     M5  0.230574  0.230574       Devoluções
5   2017     M6  0.236777  0.236777       Devoluções
6   2017     M7  0.250967  0.250967       Devoluções
7   2017     M8  0.252535  0.252535       Devoluções
8   2017     M9  0.250966  0.250966       Devoluções
9   2017    M10  0.220475  0.220475       Devoluções
10  2017    M11  0.226277  0.226277       Devoluções
11  2017    M12  0.237355  0.237355       Devoluções
12  2018     M1  1.155845  1.155845       Devoluções

str.format function

df.assign(Period=df.Period.map('M{}'.format))

    Year Period         y      yhat Contas Resultado
0   2017     M1  1.251556  1.251556       Devoluções
1   2017     M2  0.210990  0.210990       Devoluções
2   2017     M3  1.186015  1.186015       Devoluções
3   2017     M4  0.253021  0.253021       Devoluções
4   2017     M5  0.230574  0.230574       Devoluções
5   2017     M6  0.236777  0.236777       Devoluções
6   2017     M7  0.250967  0.250967       Devoluções
7   2017     M8  0.252535  0.252535       Devoluções
8   2017     M9  0.250966  0.250966       Devoluções
9   2017    M10  0.220475  0.220475       Devoluções
10  2017    M11  0.226277  0.226277       Devoluções
11  2017    M12  0.237355  0.237355       Devoluções
12  2018     M1  1.155845  1.155845       Devoluções
Sign up to request clarification or add additional context in comments.

Comments

3

One solution is convert integer values to strings and add M:

results['Period'] = 'M' + results['Period'].astype(str)

Or map with changed dictionary - keys are integers:

results['Period'] = results['Period'].map({x: 'M' + str(x) for x in range(1, 13)})

Detail:

print ({x: 'M' + str(x) for x in range(1, 13)})
{1: 'M1', 2: 'M2', 3: 'M3', 4: 'M4', 5: 'M5', 6: 'M6', 
 7: 'M7', 8: 'M8', 9: 'M9', 10: 'M10', 11: 'M11', 12: 'M12'}

print (results)
    Year Period         y      yhat Contas Resultado
0   2017     M1  1.251556  1.251556       Devolucoes
1   2017     M2  0.210990  0.210990       Devolucoes
2   2017     M3  1.186015  1.186015       Devolucoes
3   2017     M4  0.253021  0.253021       Devolucoes
4   2017     M5  0.230574  0.230574       Devolucoes
5   2017     M6  0.236777  0.236777       Devolucoes
6   2017     M7  0.250967  0.250967       Devolucoes
7   2017     M8  0.252535  0.252535       Devolucoes
8   2017     M9  0.250966  0.250966       Devolucoes
9   2017    M10  0.220475  0.220475       Devolucoes
10  2017    M11  0.226277  0.226277       Devolucoes
11  2017    M12  0.237355  0.237355       Devolucoes
12  2018     M1  1.155845  1.155845       Devolucoes

2 Comments

I got this error 'TypeError: width must be of integer type, not str'
I add astype(str) later
3

We can using map

s=pd.Series(repl_dict )
s.index=s.index.astype(int)
results['Period'] = results['Period'].map(s)

Comments

2

Convert your series from numeric to strings to match your dictionary.

In addition, I strongly recommend you use pd.Series.map, as pd.Series.replace has a relative overhead.

d = {'01': 'M1', ....}
results['Period'] = results['Period'].astype(str).str.zfill(2).map(d)

We use str.zfill(2) to zero-pad to a maximum of 2 figures.

Comments

0

Convert your column datatype first, that is why you are getting the error:

results['Period'] = results['Period'].astype(str)

results['Period'].replace({'01':'M1', '02':'M2', '03':'M3', '04': 'M4', '05': 'M5', '06': 'M6', '07': 'M7', '08': 'M8', '09':'M9', '10':'M10', '11':'M11', '12':'M12'})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.