Search code examples
pythonpandasdataframeappendcolumnsorting

Python: If dataframe has a column name containing a certain string, append column to different dataframe


I've seen this answered a lot for values, but not for the column header itself.

Say I have my original dataframe, df1:

   axx  byy  czz
0    1    2    3
1    4    5    6

And a second dataframe, df2:

   dd  ee
0   7   8
1   9  10

If dataframe 1 contains the string sequence "yy" , append the whole column (values included) to dataframe 2, so in the end for df2 I would get this:

   dd  ee  byy
0   7   8    2
1   9  10    5

How do I do this? I know it has something along the lines of df1.columns.str.contains('yy') but this returns a boolean, and I can't work out how to use that to copy over and append the entire column.


Solution

  • You can use filter for this:

    new_df = pd.concat([df2, df1.filter(like='yy')], axis=1)
    

    Output:

    >>> new_df
       dd  ee  byy
    0   7   8    2
    1   9  10    5
    

    Or df.columns.str.contains, like you were thinking:

    yy_cols = df1.columns[df1.columns.str.contains('yy')]
    new_df = pd.concat([df2, df1[yy_cols]], axis=1)