Sep-02-2019, 02:49 AM
(Sep-01-2019, 06:21 AM)Scott Wrote: I have a DF and I want to set an if statement in a function to sum a value if the first part of a field = '10'.You need to convert values to strings first and use
.str.startswith
method. Take a look at the following minimal example I just wrote:
import pandas as pd df = pd.DataFrame({"x": [100, 1000, 1000, 1919, 124], "y": [1, 2, 3, 4, 5]}) df.loc[df.x.astype(str).str.startswith('10'), 'y'].sum()