Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我有没有办法找到一个值';数据帧中的索引/位置?(python)_Python_Pandas_Dataframe_Indexing_Pandas Styles - Fatal编程技术网

我有没有办法找到一个值';数据帧中的索引/位置?(python)

我有没有办法找到一个值';数据帧中的索引/位置?(python),python,pandas,dataframe,indexing,pandas-styles,Python,Pandas,Dataframe,Indexing,Pandas Styles,我尝试在数据框中设置值及其旁边的值的样式: 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:

我尝试在数据框中设置值及其旁边的值的样式:

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)
但我无法将其旁边的值设置为样式。 所以输出是 但它看起来应该是这样的

我该怎么做?

试试这个方法:

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)
输出:


是否可以创建一个可复制的示例?这可以防止其他人不得不编写代码来重现问题。例如,对于初学者,您可以包括这行代码:
pandas\u chart\u sorted=pd.DataFrame({'Palabras':{0:papa',1:pepe',2:ja'},'Apricinoes':{0:2,1:2,2:1})
但是我们需要能够复制、粘贴代码并复制它。您想在数据框中设置整行的样式还是只设置它旁边的值?只设置它旁边的值,但我非常想学习如何设置整行。@pedro04添加了两种格式的更新。非常感谢scott!好的,谢谢!你能解释一下是关于什么的吗?我对它有点陌生。我知道您检查了de条件s以给出样式。但是条件说明了什么?哦。我在这里使用的条件是检查一个数字是否可以被6整除,如果可以,则使用
shift
标记该值和该行中的下一个值。