1

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 |by taking the middle char of the column name.

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

2
  • Are the column name lengths always 3 characters long? Commented May 6, 2015 at 22:06
  • For instance is df.append(dict(zip(df.columns, [col[1] for col in df])), ignore_index=True) what you want? Commented May 6, 2015 at 22:11

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that works great!!! though is there a way for me to specify which parts of the column name string I want, each of my actual column names are longer than in the example and I want to make a new string composed of pieces of the column name like : columname[0][6:7]+columname[0][7:11]+columname[0][24:25] = new columnname to be appended to each row entry
You just edit the list comprehension but you need to be careful that you don't index out of range but yes that would still work: df.append(dict(zip(df.columns, [col[0]+col[6:7] +col[0]+col[7:11] +col[0]+col[24:25] for col in df])), ignore_index=True)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.