0

I'm trying to append a pandas DF that should show values with dollar amounts with another DF to show percentage value (division of the dollar values)

DF1:

enter image description here

DF2:

enter image description here

I'm able to format the columns correctly if i keep the DFs separated but it fails when appending, and formats all the column as $ or %.
I'm using the following code, and would like to understand how to define the subset by "Description" column value?

opps[(opps['Name'] == name) & (opps['Description'] != 'Win Rate')].iloc[:,4:].reset_index(drop=True)\
.replace([np.inf, -np.inf], 0, regex=True)\
.append(opps[(opps['Name'] == name) & (opps['Description'] == 'Win Rate')].iloc[:,4:]).reset_index(drop=True)\
.replace([np.inf, -np.inf], 0, regex=True)\
.style\
.set_table_styles(styles)\
.format("{:.0%}", na_rep="-", subset=['YTD#'])

Thanks

Mshendy
  • 303
  • 3
  • 11

1 Answers1

0

Use a pandas IndexSlice, or just represent the data in (rows, cols) format:

df = pd.DataFrame([[1,2],[3,4]], index=["a", "b"], columns=["c", "d"])
df.style.format("{:.3f}", subset=("a", slice(None)))

       c     d
a  1.000 2.000
b      3     4

Alternatively if not using a index to index but based on columbn values, create a row boolean indexer:

df.style.format("{:.3f}", subset=(df["c"]==3, slice(None)))

       c     d
a      1     2
b  3.000 4.000
Attack68
  • 4,437
  • 1
  • 20
  • 40