Rename Columns of one DataFrame to Another (R or Python)

为君一笑 提交于 2020-03-06 11:01:12

问题


I want to rename the columns from one dataframe into the columns of another, meanwhile creating a completely new dataframe. I am not really sure how to approach this and which is why the consultation. I want to take the name of one element in the string from one column and reuse it to another. This can be either in R or python, doesn't matter too much. The rest of the string values can be fixed values.

Such as: Hm106_120.region001 1813 PKSI_GCF1813 Streptomyces_sp_Hm106 MBT13_26.region001 1813 PKSI_GCF1813 Streptomyces_sp_MBT13

Please see the example in the picture posted for better description

Thanks for the help :)Table Rename


回答1:


df2 = df1.copy()
df2.rename(columns={"GCF No": "GCF"}, inplace=True)
df2['GCF'] = 'PKSI_GCF' + df2['GCF'].astype(str)
df1[['BGC','BGC2']] = df1['BGC'].str.split('_',expand=True)
df2['Genome'] = 'Streptomyces_sp_' + df1['BGC'].astype(str)
df2.set_index('GCF', inplace=True)


来源:https://stackoverflow.com/questions/59822507/rename-columns-of-one-dataframe-to-another-r-or-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!