简体   繁体   English

在 python pandas 中应用样式后,如何删除和重新排序(重新索引)列?

[英]How do I remove and re-sort (reindex) columns after applying style in python pandas?

Is there a way to remove columns or rows after applying style in python pandas?在 python pandas 中应用样式后,有没有办法删除列或行? And re-sort them?并重新排序?

styled = df.style.apply(colorize, axis=None)
#remove _x columns
yonly = list(sorted(set(styled.columns) - set(df.filter(regex='_x$').columns)))
###Remove columns that end with "_x" here
styled.to_excel('styled.xlsx', engine='openpyxl', freeze_panes=(1,1))

Most things I tried were unavailable, ie styled.reindex(columns=yonly) returned AttributeError: 'Styler' object has no attribute 'reindex'我尝试过的大多数东西都不可用,即styled.reindex(columns=yonly)返回AttributeError: 'Styler' object has no attribute 'reindex'

styled.columns = yonly returned AttributeError: 'list' object has no attribute 'get_indexer' styled.columns = yonly返回AttributeError: 'list' object has no attribute 'get_indexer'

styled = styled[yonly] returns TypeError: 'Styler' object is not subscriptable styled = styled[yonly]返回TypeError: 'Styler' object is not subscriptable

Follow-up from Colour specific cells from two columns that don't match, using python pandas style.where (or otherwise) and export to excel 从不匹配的两列中的颜色特定单元格跟进,使用 python pandas style.where(或其他方式)并导出到 excel

After @jezrael 's comment to remove columns before styling and colouring, I got my answer :)@jezrael在样式和着色之前删除列的评论之后,我得到了答案:)

The solution was to pass an extra argument, making the original dataframe df available.解决方案是传递一个额外的参数,使原始数据帧df可用。 And coloured the dataframe df_tmp with the "_y" only.df_tmp使用“_y”为数据df_tmp着色。 :) :)

df = pd.DataFrame({
    'config_dummy1': ["dummytext"] * 10,
    'a_y': ["a"] * 10,
    'config_size_x': ["textstring"] * 10,
    'config_size_y': ["textstring"] * 10,
    'config_dummy2': ["dummytext"] * 10,
    'a_x': ["a"] * 10
})
df.at[5, 'config_size_x'] = "xandydontmatch"
df.at[9, 'config_size_y'] = "xandydontmatch"
df.at[0, 'a_x'] = "xandydontmatch"
df.at[3, 'a_y'] = "xandydontmatch"
print(df)

def color(x, extra):
    c1 = 'color: #ffffff; background-color: #ba3018'
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)

    #select only columns ends with _x and _y and sorting
    cols = sorted(extra.filter(regex='_x$|_y$').columns)
    #loop by pairs and assign style by mask
    for colx, coly in zip(cols[::2],cols[1::2]):
        #pairs columns 
        m = extra[colx] != extra[coly]
        df1.loc[m, [coly]] = c1
    return df1

yonly = list(sorted(set(df.columns) - set(df.filter(regex='_x$').columns)))
df_tmp = df[yonly]
df_tmp.style.apply(color, axis=None, extra=df).to_excel('styled.xlsx', engine='openpyxl')

Thank you wonderful people of SO!谢谢SO的好人! :D :D

I'm not sure about re-sorting, but if you only want to remove (hide) some columns use Styler's我不确定重新排序,但如果您只想删除(隐藏)某些列,请使用 Styler
hide_columns method. hide_columns方法。

For example to hide columns 'A' and 'B':例如隐藏列“A”和“B”:

hide_columns(['A', 'B'])

I had a similar scenario wherein I had to color background of a dataframe based on another dataframe.我有一个类似的场景,其中我必须根据另一个数据框为数据框的背景着色。 I created a function for coloring based on the ranges of the other dataframe as follows:我创建了一个基于其他数据框范围的着色函数,如下所示:

def colval(val, z1):
    color= 'None'
    df1= pd.DataFrame('', index= val.index, columns= val.columns) # dataframe for coloring
    colm= z1.shape
    
    for x in list(range(colm[0])):
        for y in list(range(1, colm[1])):
# check the range in the dependent dataframe
# and color the other one
             if(z1.iloc[x, y]>= 4.5): 
                df1.iloc[x, y]= 'background-color: red'
             elif(z1.iloc[x, y]<= -4.5):
                df1.iloc[x, y]= 'background-color: yellow'      
    return df1
df_tocol.style.apply(colval, axis= None, z1= diff_df)

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 重新排序列表/文本? - How to re-sort a list/text with Python? 如何在不访问数据库的情况下对内存缓存进行重新排序 - how to re-sort memcache without hitting the database 如何在Pandas中按2个multiindex列对sort_values()进行排序 - How do I sort with sort_values() by 2 multiindex columns in Pandas 应用样式器功能后如何删除列? pandas.io.formats.style.Styler.hide_columns - 不工作 - How to delete column after applying styler function? pandas.io.formats.style.Styler.hide_columns - not work 在 python 中应用巴特沃斯带通滤波器后,如何消除信号开头的大尖峰? - how do I remove large spike at the beginning of signal after applying Butterworth Bandpass filter in python? 如何重新索引多级列 - How do reindex multilevel columns 如何使用日期时间索引在pandas中进行插值重建索引? - How can I do an interpolating reindex in pandas using datetime indices? 使用Python中的Pandas,如何按agg函数创建的两列进行排序? - With Pandas in Python, how do I sort by two columns which are created by the agg function? 如何在Pandas Python中对行数据进行分组,排序和放置 - How do I groupBy, sort and put row data also in new columns in Pandas Python Python Pandas:时区转换后的Reindex DataFrame - Python Pandas: Reindex DataFrame after Timezone conversion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM