Home > Software engineering >  How do I process the elements based on their length and type in pandas?
How do I process the elements based on their length and type in pandas?

Time:05-02

A quick question.

So, let's say I have these elements

df['elements'] = [719016467.0, NaN, 12345, 333, '12A3']

The direct result is

result['elements'] = [71901, 1, 12345, 1, 1]

Basically, if it doesn't have >= 5 characters and isn't numeric, it becomes 1 and if it does, it gets reduced to 5 characters.

Thank you in advance

CodePudding user response:

Try with string operations and where:

strings = df["elements"].fillna('').astype(str)
elements = strings.str[:5].where(strings.str.len().ge(5), 1)

>>> elements
0    71901
1        1
2    12345
3        1
4        1
Name: elements, dtype: object

CodePudding user response:

You could use np.where

df['elements'] = df['elements'].astype(str)
df['elements'] = np.where(df['elements'].str.len()>=5, df['elements'].str[:5], '1').astype(int)
  • Related