简体   繁体   中英

Change some, but not all, pandas multiindex column names

Suppose I have a data frame with multiindex column names that looks like this:

     A                 B
     '1.5' '2.3' '8.4' b1
r1   1     2     3     a
r2   4     5     6     b
r3   7     8     9     10

How would I change the just the column names under 'A' from strings to floats, without modifying 'b1', to get the following?

     A                 B
     1.5   2.3   8.4   b1
r1   1     2     3     a
r2   4     5     6     b
r3   7     8     9     10

In the real use case, under 'A' there would be thousands of columns with names that should be floats (they represent the wavelengths for a spectrometer) and the data in the data frame represents multiple different observations.

Thanks!

# build the DataFrame (sideways at first, then transposed)
arrays = [['A','A','A','B'],['1.5', '2.3', '8.4', 'b1']]
tuples = list( zip(*arrays) )

data1 = np.array([[1,2,3,'a'], [4,5,6,'b'], [7,8,9,10]])
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second']) 
df = pd.DataFrame(data1.T, index=index).T

Printing df.columns gives the existing column names.

Out[84]: 
MultiIndex(levels=[[u'A', u'B'], [u'1.5', u'2.3', u'8.4', u'b1']],
       labels=[[0, 0, 0, 1], [0, 1, 2, 3]],
       names=[u'first', u'second'])

Now change the column names

# make new column titles (probably more pythonic ways to do this)
A_cols = [float(i) for i in df['A'].columns]
B_cols = [i for i in df['B'].columns]    
cols = A_cols + B_cols

# set levels
levels = [df.columns.levels[0],cols]
df.columns.set_levels(levels,inplace=True)

Gives the following output

Out[86]: 
MultiIndex(levels=[[u'A', u'B'], [1.5, 2.3, 8.4, u'b1']],
       labels=[[0, 0, 0, 1], [0, 1, 2, 3]],
       names=[u'first', u'second'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM