0

I have this dataframe a sample of it is here:

Blockquote

I'm having trouble with this code. I'm getting the below error message:

Traceback (most recent call last):   File "C:/Users/....py", line 12, in <module>
 dfF['CCRYear'] = dfF['Year'].apply(lambda x: 'True' if x['Year'] == x['MaxSS Year'] else 'False')   File "C:\Users\....py", line 3848, in apply
 mapped = lib.map_infer(values, f, convert=convert_dtype)   File "pandas\_libs\lib.pyx", line 2329, in pandas._libs.lib.map_infer   
 File "C:/Users/....py", line 12, in <lambda>
 dfF['CCRYear'] = dfF['Year'].apply(lambda x: 'True' if x['Year'] == x['MaxSS Year'] else 'False')
 TypeError: 'int' object is not subscriptable

The Columns 'Year' and 'MaxSS Year' are both int64 datatypes. So this is my code below:

import pandas as pd
import numpy as np

def cached_date_parser(s):
    if s in cache:
        return cache[s]
    dt = pd.to_datetime(s, format='%Y%m%d', coerce=True)
    cache[s] = dt
    return dt

dfF = pd.read_csv(r'C:\\Users\\....C_14.csv', parse_dates = [1], header='infer')
dfF['CCRYear'] = dfF['Year'].apply(lambda x: 'True' if x['Year'] == x['MaxSS Year'] else 'False')

1 Answer 1

1

The problem with your code is you are using apply() on a single columns and you are indexing that.

See what you doing just print the x in your lambda

df['Year'].apply(lambda x: print(x))

It will output below

2017
2018
2018
2015
2015
2015

Your code is trying to index the integer value x['Year']. In this x is integer years like 2018, 2019 etc.

Change this to

dfF['CCRYear'] = dfF['Year'].apply(lambda x: 'True' if x['Year'] == x['MaxSS Year'] else 'False')

this

dfF['CCRYear'] = dfF['Year'] == dfF['MaxSS Year']

Using np.where()

dfF['CCRYear'] = np.where(dfF['Year'] == dfF['MaxSS Year'], 'True', 'False')
Sign up to request clarification or add additional context in comments.

1 Comment

Do you think the np.where method would be a faster computation?

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.