Is there a way for me to find a value's index/location within a pandas DataFrame? (python)

68 views Asked by At

I'm trying to style a value and a value next to it in a pandas dataframe:

pandas_chart_sorted = pd.DataFrame({'Palabras': {0: 'papa', 1: 'pepe', 2: 'ja'}, 'Apariciones': {0: 2, 1: 2, 2: 1}})
    
    def filter_mark(val):
        if val in self.filters:
            color = 'red'
        else:
            color = 'black'
        return 'color: {}'.format(color)

    pandas_chart_sorted = pandas_chart_sorted.style.applymap(filter_mark)

    with pd.ExcelWriter(self.new_path) as writer:
        pandas_chart_sorted.to_excel(writer)

but I can't manage to style the value right next to it. So the output is this but it should look like this.

How can I do it?

1

There are 1 answers

2
Scott Boston On

Try this method:

df = pd.DataFrame(np.arange(25).reshape(5, -1))

def filter_mark(row):
    s = (row % 6 == 0) | (row.shift() % 6 == 0) 
    return [f'color: red' if i != False else '' for i in s]

def filter_row(row):
    return ['background: yellow'if (row % 10 == 0).any() else '' for _ in row]

df.style.apply(filter_mark, axis=1).apply(filter_row, axis=1)

Output:

enter image description here