Home > Blockchain >  Python: How to get the string values of a 2D array greater than n characters long?
Python: How to get the string values of a 2D array greater than n characters long?

Time:06-20

s = (["id" "title" "name"]
     ["1" "show" "anna"]
     ["2" "hide" "joh"]
)

I need to get the string values with more than 2 characters long.

Expected output:

s =  (["id" "title" "name"]
      [ ""   "show"  "anna"]
      [ ""   "hide"  "joh"]
)

I tried searching google but I didn't find a solution. Since the data is quite large I also tried DataFrame.iterrows or columns in pandas but it didn't get the desired result.

Can I use pandas to do this?

CodePudding user response:

Assuming that your data is a DataFrame

import pandas as pd 

df = pd.DataFrame([["1", "show", "anna"],
                   ["2", "hide","joh"]], 
                  columns=["id", "title", "name"])

You can use Series.apply to check which string values of each column have a length greater than 2 (using col.str.len() > 2), and get a boolean mask.

Then use that mask with DataFrame.where to replace the values which don't satisfy that condition with empty strings ''.

res = df.where(df.apply(lambda col: col.str.len() > 2), '')

Output:

>>> res

  id title  name
0     show  anna
1     hide   joh
  • Related