admin管理员组

文章数量:1391064

I have a df_source. It contains either three columns, namely A_1, B_1 and C_1, or six columns, namely A_1, A_2, B_1, B_2, C_1 and C_2.

I want to select only the A_* and B_* and save them into another df, namely df_new. The resulting df_new may contain either two columns or four columns.

I am thinking of doing something like the following:

df_new = df_source[["A_1", "B_1"]]
if 'A_2' in df_new.columns:
    # Also add A_2, B_2 columns in df_new, but I don't know how to add the columns to the created df_new.

Instead of

df_new = df_source[["A_1", "B_1"]]
if 'A_2' in df_new.columns:
  df_new = df_source[["A_1", "B_1", "A_2", "B_2"]]

I have a df_source. It contains either three columns, namely A_1, B_1 and C_1, or six columns, namely A_1, A_2, B_1, B_2, C_1 and C_2.

I want to select only the A_* and B_* and save them into another df, namely df_new. The resulting df_new may contain either two columns or four columns.

I am thinking of doing something like the following:

df_new = df_source[["A_1", "B_1"]]
if 'A_2' in df_new.columns:
    # Also add A_2, B_2 columns in df_new, but I don't know how to add the columns to the created df_new.

Instead of

df_new = df_source[["A_1", "B_1"]]
if 'A_2' in df_new.columns:
  df_new = df_source[["A_1", "B_1", "A_2", "B_2"]]
Share Improve this question edited Mar 17 at 7:56 mkrieger1 23.5k7 gold badges64 silver badges82 bronze badges asked Mar 16 at 15:37 Coleman YUColeman YU 735 bronze badges 2
  • This question is similar to: How to select all columns whose names start with X in a pandas DataFrame. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. In particular, check this answer for multiple prefixes – Ignatius Reilly Commented Mar 16 at 16:37
  • I think the difference lies in that I want to create df_new in a two-step process, as I will do different manipulations based on the true value on "if 'A_2' in df_new.columns". – Coleman YU Commented Mar 16 at 16:47
Add a comment  | 

1 Answer 1

Reset to default 1

You can use another list like this

first_list =[1,2,3,4,5,6]
filter_list=[]
for item in first_list:
    if item % 2 ==0:
        filter_list.append(item)

本文标签: pythonAppend columns to a df according to the column names from the source dfStack Overflow