【问题标题】:Add Text To INT In Pandas DataFrame在 Pandas DataFrame 中将文本添加到 INT
【发布时间】:2019-01-15 07:20:52
【问题描述】:
+------------+-------------+-----------------------+------------------+-------------------+---------------+------------------------------+--------+
| Technology | All Backlog | All Backlog Completed | Percent Complete | Backlog Remaining | Internal ONLY | Ext/Int & Ready For External | Non-IC |
+------------+-------------+-----------------------+------------------+-------------------+---------------+------------------------------+--------+
| Tech 1     |        2395 |                  1069 | 44.63%           |              1326 |            34 |                          219 |    822 |
+------------+-------------+-----------------------+------------------+-------------------+---------------+------------------------------+--------+

假设这个表是一个 DataFrame ...(索引被删除)

右 4 列是 INT。我希望它们也显示完成百分比,就像我在其他列中一样,这些列被格式化为显示 %。比如……

+------------+-------------+-----------------------+------------------+-------------------+---------------+------------------------------+--------------+
| Technology | All Backlog | All Backlog Completed | Percent Complete | Backlog Remaining | Internal ONLY | Ext/Int & Ready For External |    Non-IC    |
+------------+-------------+-----------------------+------------------+-------------------+---------------+------------------------------+--------------+
| Tech 1     |        2395 |                  1069 | 44.63%           |              1326 | 34 (3.18%)    | 219 (20.48%)                 | 822 (76.89%) |
+------------+-------------+-----------------------+------------------+-------------------+---------------+------------------------------+--------------+

我查看了 map、applymap 和其他...但我能得到的唯一接近的是:

+------------+-------------+-----------------------+------------------+-------------------+--------------------------------------+-----------------------------------------+----------------------------------------+
| Technology | All Backlog | All Backlog Completed | Percent Complete | Backlog Remaining |            Internal ONLY             |      Ext/Int & Ready For External       |                 Non-IC                 |
+------------+-------------+-----------------------+------------------+-------------------+--------------------------------------+-----------------------------------------+----------------------------------------+
| Tech 1     |        2395 |                  1069 | 44.63%           |              1326 | 34 (0 3.18% 1 3.18% dtype: float64%) | 219 (0 20.49% 1 20.49% dtype: float64%) | 822(0 76.89% 1 76.89% dtype: float64%) |
+------------+-------------+-----------------------+------------------+-------------------+--------------------------------------+-----------------------------------------+----------------------------------------+

我认识到它显示得很奇怪,因为它试图将一个系列而不是相应的数据附加到单元格中......因此我想要映射,但我发现没有任何东西似乎符合需要。

这是我目前正在使用的代码。

def get_IC_percomp(df:pd.DataFrame):
    """
    This function takes in the DF of the default view's data. 
    Takes the last 3 columns and divides by items completed. 
    (Checking for 0 to avoid math issues.) --- might not be possible since comparing DF columns

    Columns to perform this on:
        Internal ONLY
        Ext/Int & Ready For External
        Non-IC

    Returns a dictionary like:    {'Column Name': data_value, ...}

    """

    logger.debug('Starting get_IC_percomp()...')

    column_list = ['Internal ONLY', 'Ext/Int & Ready For External', 'Non-IC']
    new_dict = {column: None for column in column_list}    # could also do:  dict.fromkeys(keys, None)
    for col in column_list:
        #if float(df['All Backlog Completed'].applymap(float)) == 0.0:
        #    new_dict[col] = 0
        #else:
        #    new_dict[col] = df[col] / df['All Backlog Completed']
        new_dict[col] = df[col] / df['All Backlog Completed'] * 100

    logger.debug('Ending get_IC_percomp()...')
    return new_dict

def chg_font_fmt(data_dict:dict):
    """
    This function takes in a dictionary of data and returns 
    a dictionary for use in formatting the DataFrame.

    """

    new_dict = dict()
    new_dict['Percent Complete'] = '{:.2f}%'
    new_dict['Total Percent Complete'] = '{:.2f}%'
    new_dict['Historical Percent Complete'] = '{:.2f}%'
    new_dict['Sustain Percent Complete'] = '{:.2f}%'
    if data_dict is not None:
        new_dict['Internal ONLY'] = '{:}' + ' ({}%)'.format(data_dict['Internal ONLY'])
        new_dict['Ext/Int & Ready For External'] = '{:}' + ' ({}%)'.format(data_dict['Ext/Int & Ready For External'])
        new_dict['Non-IC'] = '{:}' + '({}%)'.format(data_dict['Non-IC'])

    return new_dict

percent_dict = None
if view_str.lower() == 'default':      # default view
    percent_dict = get_IC_percomp(df)

df.format(chg_font_fmt(percent_dict)).set_properties(**{'text-align': 'center'}).render()

可能在这里找到了答案:stackoverflow.com/a/54026256/10474024 但是测试一直没有结果。

【问题讨论】:

  • 花点时间阅读帮助中心的editing help。 Stack Overflow 上的格式设置与其他站点不同。您的帖子看起来越好,其他人就越容易阅读和理解。
  • 不确定这应该怎么做?无法创建表格,因为不允许使用 HTML 表格。
  • 您的列将不再被评估为 int,而是对象。可以吗?
  • 是的,此时它不再需要是 int,因为我将使用 .render() 推送到 HTML
  • 我相信我创建了一个解决方案......现在测试......如果它有效,将提供答案

标签: python pandas dataframe formatting


【解决方案1】:

需要添加新功能 ma​​p_perc - 请参见下面的代码。

def get_IC_percomp(df:pd.DataFrame):
    """
    This function takes in the DF of the default view's data. 
    Takes the last 3 columns and divides by items completed. 
    (Checking for 0 to avoid math issues.) --- might not be possible since comparing DF columns

    Columns to perform this on:
        Internal ONLY
        Ext/Int & Ready For External
        Non-IC

    Returns a dictionary like:    {'Column Name': data_value, ...}

    """

    logger.debug('Starting get_IC_percomp()...')

    column_list = ['Internal ONLY', 'Ext/Int & Ready For External', 'Non-IC']
    new_dict = {column: None for column in column_list}    # could also do:  dict.fromkeys(keys, None)
    for col in column_list:
        new_dict[col] = df[col] / df['All Backlog Completed'] * 100

    logger.debug('Ending get_IC_percomp()...')
    return new_dict

def chg_font_fmt(data_dict:dict):
    """
    This function takes in a dictionary of data and returns 
    a dictionary for use in formatting the DataFrame.

    """

    new_dict = dict()
    new_dict['Percent Complete'] = '{:.2f}%'
    new_dict['Total Percent Complete'] = '{:.2f}%'
    new_dict['Historical Percent Complete'] = '{:.2f}%'
    new_dict['Sustain Percent Complete'] = '{:.2f}%'
    if data_dict is not None:
        new_dict['Internal ONLY'] = '{:}' + ' ({}%)'.format(data_dict['Internal ONLY'])
        new_dict['Ext/Int & Ready For External'] = '{:}' + ' ({}%)'.format(data_dict['Ext/Int & Ready For External'])
        new_dict['Non-IC'] = '{:}' + '({}%)'.format(data_dict['Non-IC'])

    return new_dict

def map_perc(df:pd.DataFrame, data_dict:dict):
    """
    This function takes in a df and dictionary.
    Returns DF.

    """

    def use_idx_val(row):
        """
        https://stackoverflow.com/a/54026256/10474024

        """

        idx = row.name

        row['Internal ONLY'] = '{} ({:.2f}%)'.format(row['Internal ONLY'], data_dict['Internal ONLY'][idx])
        row['Ext/Int & Ready For External'] = '{} ({:.2f}%)'.format(row['Ext/Int & Ready For External'], data_dict['Ext/Int & Ready For External'][idx])
        row['Non-IC'] = '{} ({:.2f}%)'.format(row['Non-IC'], data_dict['Non-IC'][idx])

        return row

    df = df.apply(use_idx_val, axis=1)

    return df

percent_dict = None
if view_str.lower() == 'default':      # default view
    percent_dict = get_IC_percomp(df)
    df = map_perc(df, percent_dict)

df.format(chg_font_fmt(percent_dict)).set_properties(**{'text-align': 'center'}).render()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多