Home > database >  Extract dynamic and static columns from dataFrame
Extract dynamic and static columns from dataFrame

Time:10-07

I am working on one requirement in which few columns are fixed and few columns are dynamic. i am stuck on the dataframe filter part.

sample dataset:

ZR  ER   WS  TG  2/3 5/3  2/6 5/50

q   r    e   q   f    w    q    e

NOTE: numeric columns are dynamic(i.e count will change in each file) I want to filter columns (ZR, ER and all numeric columns).

I tried with .iloc[] but is there any other way that helps me to extract the dataset.

Expected Format:

ZR  ER  2/3  5/3  2/6  5/50
q   r   f     w     q   e

CodePudding user response:

If need test columns by list by Index.isin and numeric for test if exist digit use:

m = df.columns.isin(['ZR', 'ER']) | df.columns.str.contains('^\d /\d $')
df = df.loc[:, m]

print (df)
  ZR ER 2/3 5/3 2/6 5/50
0  q  r   f   w   q    e

CodePudding user response:

Try this :

df[['ZR','ER',23,5,2]]

CodePudding user response:

You can use filter:

out = df.filter(regex=r'^((ZR|ER)$|\d)')
print(out)

# Output:
  ZR ER 2/3 5/3 2/6 5/50
0  q  r   f   w   q    e

A more accuracy expression:

out = df.filter(regex=r'^(ZR|ER|\d /\d )$')
print(out)

# Output:
  ZR ER 2/3 5/3 2/6 5/50
0  q  r   f   w   q    e
  • Related