[英]How to compare a list with dataframe column headers and substitute the header's name?
有一个df
df_example =
id city street house flat
0 NY street_ny 111 01
1 LA street_la 222 02
2 SF street_sf 333 03
3 Vegas street_vg 444 04
4 Boston street_bs 555 05
并且在数据库中存在一个表,其中每个列名都与列 id 匹配(withoit id 列)
sql_table (as df) =
column_name column_id
city 0
street 1
house 2
flat 3
我需要用 sql_table 中的列 ID 替换 df_example 列名
像这样
id 0 1 2 3
0 NY street_ny 111 01
1 LA street_la 222 02
2 SF street_sf 333 03
3 Vegas street_vg 444 04
4 Boston street_bs 555 05
到目前为止,我得到了没有 id 列名的列名列表
column_names_list = list(df_example)[1:]
column_names_list = ['city', 'street', 'house', 'flat']
但是如何进行我不知道
.isin 方法并不是我真正需要的
感谢任何帮助
使用zip
创建的字典rename
:
df_example = df_example.rename(columns=dict(zip(df['column_name'], df['column_id'])))
print (df_example)
id 0 1 2 3
0 0 NY street_ny 111 1
1 1 LA street_la 222 2
2 2 SF street_sf 333 3
3 3 Vegas street_vg 444 4
4 4 Boston street_bs 555 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.