0

I am trying to rename my columns from a df exported from pandas by doing a Header array like this.

Header_1 = [ 'Time', 'Tx/Rx', 'Channel', 'ID', 'Bits', 'A', 'B' ]
Frame_1 = Frame_1.rename(columns = Header_1)

but I get the following TypeError: 'list' object is not callable

I know that I can name my headers directly from pandas as:

df = pd.read_csv('df.txt', header = 15, sep=' ',
                    names = ['Time', 'Tx/Rx', 'Channel', 'ID', 'Bits', 'A', 'B'])

but because my df is split into 2 different bytes with different IDs I exported the df and made two dfs, one for each byte frame by the name of the ID. They are like this:

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

And want them like this:

     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

And I split them like this:

Frame_1 = df.loc[df['ID'] == '0010']
Frame_2 = df.loc[df['ID'] == '0011']

So now I have the proper df for each "ID" but I cannot name the headers accordingly for each byte :(

falo8056
  • 85
  • 13

4 Answers4

3

pd.DataFrame.rename(columns = mapper) expects mapper to be a dictionary or a callable. A list is neither of them, hence the error.

What you need is just:

Frame_1.columns = Header_1

Because you can directly assign an iterable to the columns attribute of a DataFrame.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

You can set your dataframe columns like this.

Header_1 = [ 'Time', 'Tx/Rx', 'Channel', 'ID', 'Bits', 'A', 'B' ]
Frame_1.columns = Header_1
everfight
  • 420
  • 3
  • 10
0

it takes dict Frame_1.rename(columns={'old_name_1': 'new_name_1', 'old_name_2': 'new_name_2'})

From docs: columns dict-like or function Alternative to specifying axis (mapper, axis=1 is equivalent to columns=mapper).

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rename.html

drd
  • 575
  • 2
  • 8
0

use ZIP

header = ['share','depmap-Ense','Ense-depmap']
df.rename(dict(zip(df.columns,header)),axis=0)