I want to highlight the row if the credits in df equal the debits df_cb.
My previous code is just filling in the NaNs with 0.
df = df.fillna(0)
df[['Debits (£)', 'Credit(£)']] = df[['Debits (£)',
'Credit(£)']].replace(',','',regex=True).astype(float)
df_cb = df_cb.fillna(0)
df_cb[['Debits (£)', 'Credit(£)']] = df_cb[['Debits (£)',
'Credit(£)']].replace(',','',regex=True).astype(float)
I've applied the following code:
df.style.apply(lambda x: (["background: yellow" if (df['Credit(£)'].equals(df_cb['Debits (£)']))else "" for df in df_cb]), axis = 1)<
df_cb.style.apply(lambda x: (["background: yellow" if (df['Credit(£)'].equals(df_cb['Debits (£)'])) else "" for df_cb in df]), axis = 1)
However, I receive this error:
TypeError: string indices must be integers
I am definitely not using strings, I've assigned the values to be floats as they're decimals.
So why is telling me they need to be integers for an if statement?
Many thanks,
[EDIT - Full Traceback below]:
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/IPython/core/formatters.py in __call__(self, obj)
343 method = get_real_method(obj, self.print_method)
344 if method is not None:
--> 345 return method()
346 return None
347 else:
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/io/formats/style.py in _repr_html_(self)
270 """
271 if get_option("styler.render.repr") == "html":
--> 272 return self.to_html()
273 return None
274
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/io/formats/style.py in to_html(self, buf, table_uuid, table_attributes, sparse_index, sparse_columns, bold_headers, caption, max_rows, max_columns, encoding, doctype_html, exclude_styles, **kwargs)
1176 encoding = encoding or get_option("styler.render.encoding")
1177 # Build HTML string..
-> 1178 html = obj._render_html(
1179 sparse_index=sparse_index,
1180 sparse_columns=sparse_columns,
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/io/formats/style_render.py in _render_html(self, sparse_index, sparse_columns, max_rows, max_cols, **kwargs)
155 Generates a dict with necessary kwargs passed to jinja2 template.
156 """
--> 157 self._compute()
158 # TODO: namespace all the pandas keys
159 d = self._translate(sparse_index, sparse_columns, max_rows, max_cols)
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/io/formats/style_render.py in _compute(self)
198 r = self
199 for func, args, kwargs in self._todo:
--> 200 r = func(self)(*args, **kwargs)
201 return r
202
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/io/formats/style.py in _apply(self, func, axis, subset, **kwargs)
1441 result = data.apply(func, axis=0, **kwargs)
1442 else:
-> 1443 result = data.T.apply(func, axis=0, **kwargs).T # see GH 42005
1444
1445 if isinstance(result, Series):
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwargs)
8831 kwargs=kwargs,
8832 )
-> 8833 return op.apply().__finalize__(self, method="apply")
8834
8835 def applymap(
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/apply.py in apply(self)
725 return self.apply_raw()
726
--> 727 return self.apply_standard()
728
729 def agg(self):
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/apply.py in apply_standard(self)
849
850 def apply_standard(self):
--> 851 results, res_index = self.apply_series_generator()
852
853 # wrap results
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/apply.py in apply_series_generator(self)
865 for i, v in enumerate(series_gen):
866 # ignore SettingWithCopy here in case the user mutates
--> 867 results[i] = self.f(v)
868 if isinstance(results[i], ABCSeries):
869 # If we have a view on v, we need to make a copy because
/var/folders/63/d4r2059n7z5_v4vbtw244jp00000gp/T/ipykernel_10032/2672574164.py in <lambda>(x)
1 df.style.apply(lambda x: (["background: yellow" if (df['Credit(£)'] == (df_cb['Debits (£)']))else "" for df in df_cb]), axis = 1)
----> 2 df_cb.style.apply(lambda x: (["background: yellow" if (df['Credit(£)']==(df_cb['Debits (£)'])) else "" for df_cb in df]), axis = 1)
/var/folders/63/d4r2059n7z5_v4vbtw244jp00000gp/T/ipykernel_10032/2672574164.py in <listcomp>(.0)
1 df.style.apply(lambda x: (["background: yellow" if (df['Credit(£)'] == (df_cb['Debits (£)']))else "" for df in df_cb]), axis = 1)
----> 2 df_cb.style.apply(lambda x: (["background: yellow" if (df['Credit(£)']==(df_cb['Debits (£)'])) else "" for df_cb in df]), axis = 1)
TypeError: string indices must be integers
<pandas.io.formats.style.Styler at 0x10ecaf5e0> ```