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
1 Answer
Reset to default 1You 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
版权声明:本文标题:python - Append columns to a df according to the column names from the source df - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744594860a2614713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论