0

I want to format only specific columns of the dataframe. Right now styling gets applied to every column, this can be done using subset parameter in df.style.background_gradient() as per documentation. I tried to do subset = df.iloc[:, 3:] as I want to apply styling to column 3 onwards but I am getting too many indexers error.

import pandas as pd
import seaborn as sns

filename = r'c:\Users\91956\Desktop\time_50.csv'
df = pd.read_csv(filename)
cm = sns.light_palette("green", as_cmap=True)
s = df.style.background_gradient(cmap=cm, axis = 1)

html = s.render()
with open("output.html","w") as fp:
    fp.write(html)

what is the correct way to write subset parameter so that styling applies only to the columns from index 3 afterwards.

after doing necessary changes.

import pandas as pd import seaborn as sns

filename = r'c:\Users\91956\Desktop\time_50.csv'
df = pd.read_csv(filename,index_col = 0)
cm = sns.light_palette("green", as_cmap=True)
select_col = df.columns[2:]
s = df.style.background_gradient(cmap=cm, axis = 1,subset=select_col)

html = s.render()
with open("output.html","w") as fp:
    fp.write(html)
1
  • df = pd.read_csv(filename,index_col=0) solved the issue of unnamed column Commented Dec 17, 2020 at 10:16

2 Answers 2

2

To remove 'Unnamed 0' you can use :

df.set_index('Ticker')
del df['Unnamed: 0']

To use subset :

select_col = df.columns[3:]
s = df.style.background_gradient(cmap=cm, axis = 1,subset=select_col)
Sign up to request clarification or add additional context in comments.

1 Comment

i want to add styling to columns from 13:20 onwards, is it possible to write select_col using index so that all columns from 13:20 onwards get selected
0

a quick and easy way to drop the unnamed column is:

new_df = df.loc[:, ~df.columns.str.contains('^Unnamed')]

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.