Home > Enterprise >  Is there a way to use .loc on column names instead of the values inside the columns?
Is there a way to use .loc on column names instead of the values inside the columns?

Time:02-22

I am wondering if there is a way to use .loc to check to sort df with certain column names == something else on another df. I know you can usually use it to check if the value is == to something, but what about the actual column name itself?

ex.

df1 = [ 0, 1, 2, 3]
df2 .columns = [2,4,6]

Is there a way to only display df2 values where the column name is == df1 without hardcoding it and saying df2.loc[:, ==2]?

CodePudding user response:

IIUC, you can use df2.columns.intersection to get columns only present in df1:

>>> df1
          A         B         D         F
0  0.431332  0.663717  0.922112  0.562524
1  0.467159  0.549023  0.139306  0.168273

>>> df2
          A         B         C         D         E         F
0  0.451493  0.916861  0.257252  0.600656  0.354882  0.109236
1  0.676851  0.585368  0.467432  0.594848  0.962177  0.714365

>>> df2[df2.columns.intersection(df1.columns)]
          A         B         D         F
0  0.451493  0.916861  0.600656  0.109236
1  0.676851  0.585368  0.594848  0.714365

CodePudding user response:

One solution:

df3 = df2[[c for c in df2.columns if c in df1]]
  • Related