Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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 使用df.style.applymap为单元格着色';s多页excel的背景_Python_Excel_Pandas - Fatal编程技术网

Python 使用df.style.applymap为单元格着色';s多页excel的背景

Python 使用df.style.applymap为单元格着色';s多页excel的背景,python,excel,pandas,Python,Excel,Pandas,MRE是在来自的帮助下创建的,关于层次索引的惊人摘要 MRE: 使用一个df并根据条件对单元格背景着色效果很好,但当我尝试将其应用于多张excel时,它似乎不起作用 这是我的密码: def coloring(val): color = '#EDFFE7' if val in lst else 'white' return f"background-color: {color}" groups = ["g1", "g2"

MRE是在来自的帮助下创建的,关于层次索引的惊人摘要

MRE:

使用一个df并根据条件对单元格背景着色效果很好,但当我尝试将其应用于多张excel时,它似乎不起作用

这是我的密码:

def coloring(val):
    color = '#EDFFE7' if val in lst else 'white'
    return f"background-color: {color}"


groups = ["g1", "g2"]
writer = pd.ExcelWriter("test.xlsx", engine="xlsxwriter")
for g in groups:
    df = all_df.loc[all_df[("Sue","group")] == g].copy()
    df.style.applymap(coloring).to_excel(writer, sheet_name=g)
writer.save()
这个


另外,如何在applymap方法中为subset参数添加索引?

似乎您需要将两行链接起来,因为
df.style.applymap(着色)
没有重新分配:

df.style.applymap(coloring).to_excel(writer, sheet_name=g)
相反:

df.style.applymap(coloring)
df.to_excel(writer, sheet_name=g)
或分配回:

df = df.style.applymap(coloring)
df.to_excel(writer, sheet_name=g)
编辑:

对于我来说,如果列表中的值是整数,则工作正常,因为如果对混合数据使用
np.array
,则带数字的字符串可以将所有数据转换为对象:

index = pd.MultiIndex.from_product([[2013, 2014,2015, 2016]],
                                   names=['year'])
columns = pd.MultiIndex.from_product([['Bob', 'Guido', 'Sue'], ['HR', 'group']])

data = np.array([[1,2,3,4,5,"g1"],
                 [3,6,1,3,2,"g2"],
                 [3,6,1,2,3,"g1"],
                 [6,7,8,11,23,"g2"]])

all_df = pd.DataFrame(data, index=index, columns=columns)

print (all_df.dtypes)

Bob    HR       object
       group    object
Guido  HR       object
       group    object
Sue    HR       object
       group    object
dtype: object
因此,如果将嵌套列表传递给
DataFrame
对我来说都很好:

index = pd.MultiIndex.from_product([[2013, 2014,2015, 2016]],
                                   names=['year'])
columns = pd.MultiIndex.from_product([['Bob', 'Guido', 'Sue'], ['HR', 'group']])

data = [[1,2,3,4,5,"g1"],
        [3,6,1,3,2,"g2"],
        [3,6,1,2,3,"g1"],
        [6,7,8,11,23,"g2"]]

all_df = pd.DataFrame(data, index=index, columns=columns)

print (all_df.dtypes)
Bob    HR        int64
       group     int64
Guido  HR        int64
       group     int64
Sue    HR        int64
       group    object
dtype: object


df.style.applymap(着色).to\u excel(writer,sheet\u name=g)
似乎仍然不起作用。并尝试分配回方法。@Ambleu-是否可以为测试创建一些数据样本?嗯。。。实际上是在我刚刚创作的作品中使用数据。唯一不同的是,我的原始数据是多索引数据帧。让我尝试创建多索引MRE并尝试一下。我已经用新的MRE更新了我的问题,很抱歉,我必须学习如何创建多索引列dataframe。
index = pd.MultiIndex.from_product([[2013, 2014,2015, 2016]],
                                   names=['year'])
columns = pd.MultiIndex.from_product([['Bob', 'Guido', 'Sue'], ['HR', 'group']])

data = [[1,2,3,4,5,"g1"],
        [3,6,1,3,2,"g2"],
        [3,6,1,2,3,"g1"],
        [6,7,8,11,23,"g2"]]

all_df = pd.DataFrame(data, index=index, columns=columns)

print (all_df.dtypes)
Bob    HR        int64
       group     int64
Guido  HR        int64
       group     int64
Sue    HR        int64
       group    object
dtype: object
def coloring(val):
    color = '#EDFFE7' if val in lst else 'white'
    return f"background-color: {color}"

writer = pd.ExcelWriter("test.xlsx", engine="xlsxwriter")

groups = ["g1", "g2"]
lst = [1,2,3]


for g in groups:
    df = all_df.loc[all_df[("Sue","group")] == g].copy()
    #print (df)
    df.style.applymap(coloring).to_excel(writer, sheet_name=g)

writer.save()