简体   繁体   中英

Python: create new row based on column names in DataFrame

I would like to know how to make a new row based on the column names row in a python dataframe, and append it to the same dataframe.

example

df = pd.DataFrame(np.random.randn(10, 5),columns=['abx', 'bbx', 'cbx', 'acx', 'bcx'])

I want to create a new row based on the column names that gives: b | b | b | c | c | b | b | b | c | c | by taking the middle char of the column name.

the idea is to use that new row, later, for multi-indexing the columns.

I'm assuming this is what you want as you've not responded, we can append a new row by creating a dict from zipping the df columns and a list comprehension of the middle character (assuming that column name lengths are 3):

In [126]:

df.append(dict(zip(df.columns, [col[1] for col in df])), ignore_index=True)
Out[126]:
          abx         bbx        cbx        acx         bcx
0   -0.373421  -0.1005462 -0.8280985 -0.1593167    1.335307
1    1.324328  -0.6189612  -0.743703  0.9419248    1.282682
2   0.3730312 -0.06697892   1.113707 -0.9691056    1.779643
3  -0.6644958    1.379606 -0.3751724  -1.135034   0.3287292
4   0.4406139  -0.5767996 -0.2267589  -1.384412 -0.03038372
5   -1.242734   -0.838923 -0.6724592   1.405247  -0.3716862
6   -1.682637    -1.69309  -1.291833   1.781704   0.6321988
7  -0.5793783  -0.6809975    1.03502 -0.6498381   -1.124236
8    1.589016    1.272961  -1.968225  0.5515182   0.3058628
9   -2.275342    2.892237   2.076253 -0.1422845 -0.09776171
10          b           b          b          c           c

ix --- lets you read the entire row-- you just say which ever row you want. then you get your columns and assign them to the raw you want.

See the example below.

virData = DataFrame(df)

virData.columns = virData.ix[1].values

virData.columns

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