S Sidoli-Price
S Sidoli-Price

Reputation: 21

Subset Extraction from Dataframe

I imported a Dataframe using pandas then wrote some code (as below) to create an additional column. I need to extract a subset of the newly added column and then create an if statement that will allow me to look at the last 2 values of the integers and apply the following logic:

This is the code I've written so far to create the additional column:

max_items['ind_short'] = ((max_items['Item'] % 1000).astype(int))

Can anyone help?

Upvotes: 2

Views: 60

Answers (1)

jpp
jpp

Reputation: 164843

Assuming by last number within the column you mean the units value of an integer, you can use the modulo operator %. Then use numpy.where to perform your mapping:

df = pd.DataFrame({'A': [6357, 1325, 549, 12312, 19]})

df['B'] = np.where(df['A'] % 10 == 9, 2, 1)

print(df)

       A  B
0   6357  1
1   1325  1
2    549  2
3  12312  1
4     19  2

Upvotes: 2

Related Questions