Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the columns name from a tuple to string?

Tags:

python

pandas

I have used pd.pivot_table in pandas dataframe, and the columns names becomes tuples like ('A1', 'B1'), ('A1', 'B2')... and I want them to be like 'A1_B1', 'A1_B2'...

I tried to use df.columns.values[i] = df.columns.values[i][0] + '_' + df6.columns.values[i][1], and tried rename as well.

When I checked df.columns.values, the columns' names changed, but when I cannot use these names to do indexing. I am new to python, so might not know the difference between column names and column indices.

Can anyone help me? Thanks!

like image 571
Yilun Qian Avatar asked Feb 06 '17 04:02

Yilun Qian


Video Answer


2 Answers

Use list comprehension:

df.columns = ['{}_{}'.format(x[0], x[1]) for x in df.columns]
print(df)
   A1_B1  A2_B1  A1_B2  A2_B2
0      0      1      2      3
1      4      5      6      7

Or:

df.columns = ['_'.join(x) for x in df.columns]
print(df)
   A1_B1  A2_B1  A1_B2  A2_B2
0      0      1      2      3
1      4      5      6      7
like image 161
jezrael Avatar answered Oct 11 '22 16:10

jezrael


setup

df = pd.DataFrame(
    np.arange(8).reshape(2, 4),
    columns=[('A1', 'B1'), ('A2', 'B1'), ('A1', 'B2'), ('A2', 'B2')])

print(df)

   (A1, B1)  (A2, B1)  (A1, B2)  (A2, B2)
0         0         1         2         3
1         4         5         6         7

rename

df.rename(columns='_'.join, inplace=True)
print(df)

   A1_B1  A2_B1  A1_B2  A2_B2
0      0      1      2      3
1      4      5      6      7

map

df.columns = df.columns.map('_'.join)
print(df)

   A1_B1  A2_B1  A1_B2  A2_B2
0      0      1      2      3
1      4      5      6      7
like image 43
piRSquared Avatar answered Oct 11 '22 15:10

piRSquared