我正试图通过执行这样的Header数组来重命名从pandas导出的df中的列。
Header_1 = [ 'Time', 'Tx/Rx', 'Channel', 'ID', 'Bits', 'A', 'B' ]
Frame_1 = Frame_1.rename(columns = Header_1)
但我得到了以下TypeError: 'list' object is not callable
我知道我可以直接从panda中命名我的标题为:
df = pd.read_csv('df.txt', header = 15, sep=' ',
names = ['Time', 'Tx/Rx', 'Channel', 'ID', 'Bits', 'A', 'B'])
但是,因为我的df被分成了两个不同的ID字节,所以我导出了df并制作了两个dfs,每个字节帧用ID的名称命名。它们是这样的:
14:12:59:0190 Rx 1 0010 8 185 0.0
14:12:59:2150 Rx 1 0011 8 138 184.0
14:12:59:4110 Rx 1 0010 8 185 0.0
14:12:59:6070 Rx 1 0011 8 135 184.0
14:12:59:8030 Rx 1 0010 8 185 0.0
14:12:59:9990 Rx 1 0011 8 135 184.0
想要他们这样:
Time Tx/Rx Channel ID Bits A
14:12:59:0190 Rx 1 0010 8 185 0.0
14:12:59:4110 Rx 1 0010 8 185 0.0
14:12:59:8030 Rx 1 0010 8 185 0.0
Time Tx/Rx Channel ID Bits B
14:12:59:2150 Rx 1 0011 8 138 184.0
14:12:59:6070 Rx 1 0011 8 135 184.0
14:12:59:9990 Rx 1 0011 8 135 184.0
我把它们分成这样:
Frame_1 = df.loc[df['ID'] == '0010']
Frame_2 = df.loc[df['ID'] == '0011']
因此,现在我为每个"ID"都有了合适的df,但我不能为每个字节相应地命名标题:(
pd.DataFrame.rename(columns = mapper)
希望mapper
是字典或可调用的。两者都不是列表,因此出现了错误。
你只需要:
Frame_1.columns = Header_1
因为您可以直接将可迭代项分配给DataFrame的columns
属性。
您可以这样设置数据帧列。
Header_1 = [ 'Time', 'Tx/Rx', 'Channel', 'ID', 'Bits', 'A', 'B' ]
Frame_1.columns = Header_1
需要dictFrame_1.rename(columns={'old_name_1': 'new_name_1', 'old_name_2': 'new_name_2'})
来自文档:列类似dict或函数指定轴(mapper,axis=1相当于columns=mapper(的替代方法
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rename.html
使用ZIP
header = ['share','depmap-Ense','Ense-depmap']
df.rename(dict(zip(df.columns,header)),axis=0)