【发布时间】:2022-09-22 23:03:57
【问题描述】:
我想用 df1 的列名重命名 df2 的列名并打印新的 df2 数据框。 我还想从新的 df2 中删除 \"df1_cols_to_rename_df2\" 中未列出的列
import pandas as pd
data1 = {\'first_column\': [\'1\', \'2\', \'2\'],
\'second_column\': [\'1\', \'2\', \'2\'],
\'third column\':[\'1\', \'2\', \'2\'],
\'fourth_column\':[\'1\', \'2\', \'2\'],
\'fifth_column\':[\'1\', \'2\', \'2\'],
}
data2 = {\'1st_column\': [\'1\', \'2\', \'4\'],
\'some_column\': [\'1\', \'2\', \'2\'],
\'3rd_column\':[\'1\', \'2\', \'2\'],
\'4th_column\':[\'7\', \'2\', \'2\'],
\'5th_column\':[\'1\', \'4\', \'2\'],
\'2nd_column\':[\'1\', \'5\', \'3\'],
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
df1_cols_to_rename_df2 = {\'first_column\':[\'1st_column\'], \'second_column\':[\'2nd_column\'], \'third column\':[\'3rd_column\'],\'fourth_column\':[\'4th_column\']]
所以这将是预期的输出
-
您是如何在预期输出中选择第五列的?
-
我的错误我刚刚编辑了它,正是我不希望新 df2 输出中的第五列
标签: python pandas list dataframe rename