Saurabh Kumar Karn
Saurabh Kumar Karn

Reputation: 111

Subsetting data in pandas with complex operations

I have a dataset which looks like this:

year_week  selling_amount 
201012     43 
201112     44
-1-1       56

Now there is an anomaly here. One simple way of checking is if year_week has any entry which has size less than 6. How can we do that in pandas ?

Upvotes: 0

Views: 70

Answers (3)

RTB
RTB

Reputation: 86

How about:

criterion = df['year_week'].map(lambda x: len(x)< 6)
df[criterion].index.values

This works if you leave the dtype as a string and then change it to an int later.

Upvotes: 1

Nicholas Aldershof
Nicholas Aldershof

Reputation: 265

As Bren said, you can find the values with

 df.year_week.str.len() < 6

where df is the name of your dataframe. You can then drop those values and recast the remaining values as integers like follows:

 df.drop(df.year_week.str.len() < 6, inplace=True)
 df.year_week = df.year_week.astype(int)

Upvotes: 0

BrenBarn
BrenBarn

Reputation: 251428

You can use df.year_week.str.len() < 6 to find rows with a year_week whose length is less than 6.

Upvotes: 1

Related Questions