Jun-24-2019, 11:14 AM
(Jun-24-2019, 07:05 AM)Scott Wrote: Does anyone know how I can do a slice on the first two digits of the int column (HS_code) and if those first two number = 02 then sum up imports_vfd.
If the column is of integer type, any number in it can't start with 0. So,
Hs_code
seems to be of string dtype.Hope the following example helps you:
import pandas as pd df = pd.DataFrame({'x': ['02', '34', '02'], 'y':[1,2,3]}) df[df.x.str.startswith('02')].y.sum()Note, trying to initialize the df as follows
df = pd.DataFrame({'x': [02, 34, 02], 'y':[1, 2, 3]})will yield an error.