Smiley1000
Smiley1000

Reputation: 274

How to hide axis labels in python pandas dataframe?

I've used the following code to generate a dataframe which is supposed to be the input for a seaborn plot.

data_array = np.array([['index', 'value']])
for x in range(len(value_list)):
    data_array = np.append(data_array, np.array([[int((x + 1)), int(value_list[x])]]), axis = 0)
data_frame = pd.DataFrame(data_array)

The output looks something like this:

      0       1
0 index  values
1     1 value_1
2     2 value_2
3     3 value_3

However, with this dataframe, seaborn returns an error. When comparing my data to the examples, I see that the first row is missing. The samples, being loaded in with load_dataset(), look something like this:

0 index  values
1     1 value_1
2     2 value_2
3     3 value_3

How do I remove the first row of axis labels of my dataframe so that it looks like the samples provided? Removing the first row removes the strings "index" and "values", but not the axis label.

Upvotes: 0

Views: 2635

Answers (4)

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31993

just slice your dataframe

df =data_frame[2:]
df.columns = data_frame.iloc[1] --it will set the column name

Upvotes: 1

user10325516
user10325516

Reputation:

Don't know what value_list is. However I would recommend another way to create dataframe:

import pandas as pd

value_list = ['10', '20', '30']

data_frame = pd.DataFrame({
    'index': range(len(value_list)),
    'value': [int(x) for x in value_list]})

data_frame:

   index value
0      0    10
1      1    20
2      2    30

Now you can easily change dataframe index and 'index' column:

data_frame.loc[:, 'index'] += 1
data_frame.index += 1

data_frame:

   index value
1      1    10
2      2    20
3      3    30

Upvotes: 1

Anonymous
Anonymous

Reputation: 699

Try:

new_header = df.iloc[0] #grab the first row for the header
df = df[1:] #take the data less the header row
df.columns = new_header #set the header row as the df header

Upvotes: 1

Yevhen Kuzmovych
Yevhen Kuzmovych

Reputation: 12140

Numpy thinks that index and values row is also a row of the values of the dataframe and not the names of the columns.

I think this would be more pythonic way of doing this:

pd.DataFrame(list(enumerate(value_list, 1)), columns=['index', 'values'])

Upvotes: 2

Related Questions