How to set the pandas dataframe data left/right alignment?

后端 未结 6 989
礼貌的吻别
礼貌的吻别 2020-11-30 05:23

I use pd.set_option(\"display.colheader_justify\",\"right\") to set the column header. But I can\'t find the option for data by pd.describe_option()

6条回答
  •  青春惊慌失措
    2020-11-30 06:20

    I wrapped @Hagbard's answer in a function to use it whenever I wish to display a pandas dataframe consisting English text on a notebook cell:

    from pandas import DataFrame
    
    
    def left_align(df: DataFrame):
        left_aligned_df = df.style.set_properties(**{'text-align': 'left'})
        left_aligned_df = left_aligned_df.set_table_styles(
            [dict(selector='th', props=[('text-align', 'left')])]
        )
        return left_aligned_df
    
    

    To show a dataframe, I simply write this:

    left_align(df.head())
    
    

    Caution: For large datasets, it prints all the rows and columns of df without any abstraction, so Jupyter crashes! That's why I use it with .head() or .tail() or some other limit.)

提交回复
热议问题