Skip to content
Advertisement

Python Pandas Setting Dataframe index and Column names from an array

Lets say I have data loaded from an spreadsheet:

df = pd.read_csv('KDRAT_2012.csv', index_col=0, encoding = "ISO-8859-1",)

      0     1     2     3     4     5     6     7     8     9

0 -5.53 -6.69 -6.29 -5.76 -7.74 -7.66 -6.27 -4.13 -3.08  0.00
1 -5.52 -6.68 -6.28 -5.75 -7.73 -7.65 -6.26 -4.12 -3.07  0.01
2 -4.03 -5.19 -4.79 -4.26 -6.24 -6.16 -4.77 -2.63 -1.58  1.50
3  0.11 -1.05 -0.65 -0.12 -2.10 -2.02 -0.63  1.51  2.56  5.64
4  0.23 -0.93 -0.53  0.00 -1.98 -1.90 -0.51  1.63  2.68  5.76
5 -2.53 -3.69 -3.29 -2.76 -4.74 -4.66 -3.27 -1.13 -0.08  3.00

[6 rows x 10 columns]

and I have a the names in another dataframe for the rows and the columns for example

colnames = pd.DataFrame({'Names': ['A', 'B', 'C', 'D', 'E', 'F'],
                    'foo': [0, 1, 0, 0, 0, 0]})

Is there a way I can set the values in colnames['Names'].value as the index for df? and is there a way to do this for column names?

Advertisement

Answer

How about df.index = colnames['Names'] for example:

In [77]: df = pd.DataFrame(np.arange(18).reshape(6,3))

In [78]: colnames = pd.DataFrame({'Names': ['A', 'B', 'C', 'D', 'E', 'F'],
                    'foo': [0, 1, 0, 0, 0, 0]})

In [79]: df.index = colnames['Names']

In [80]: df
Out[80]: 
        0   1   2
Names            
A       0   1   2
B       3   4   5
C       6   7   8
D       9  10  11
E      12  13  14
F      15  16  17

[6 rows x 3 columns]
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement