python - 根据现有列中的值计算新列

标签 python pandas

我有以下数据框:

df = pd.DataFrame(
    {
        "customer": ['c1', 'c2', 'c3', 'c4', 'c5'],
        "contract_year": [2018, 2020, 2019, 2018, 2019],
        "amount": [3000, 1000, 3000, 6000, 6000],
        "term": [3, 1, 2, 3, 3]
    }
)

    customer    contract_year   amount  term
0   c1          2018            3000    3
1   c2          2020            1000    1
2   c3          2019            3000    2
3   c4          2018            6000    3
4   c5          2019            6000    3

我的目标是:对于每个客户,将金额除以“期限”年数;例如: 客户c1,将付款

df["amount"]/df["term"] 

从“contract_year”开始的下一个“term”年。这些金额应在每个付款年度的新列中。

最终结果应如下所示:

    customer    contract_year   amount  term    2018   2019   2020    2021
0   c1          2018            3000    3       1000   1000   1000
1   c2          2020            1000    1                     1000
2   c3          2019            3000    2              1500   1500
3   c4          2018            6000    3       2000   2000   2000
4   c5          2019            6000    3              2000   2000    2000

非常感谢!

最佳答案

让我们这样做:

s = df.reindex(df.index.repeat(df['term']))
s['val']  = s['amount'].floordiv(s['term'])
s['year'] = s['contract_year'] + s.groupby(level=0).cumcount()

s.pivot_table('val', [*df.columns], 'year', aggfunc='first').reset_index()

详细信息:

使用index.repeat重新索引数据帧:

print(s)

  customer  contract_year  amount  term
0       c1           2018    3000     3
0       c1           2018    3000     3
0       c1           2018    3000     3
1       c2           2020    1000     1
2       c3           2019    3000     2
2       c3           2019    3000     2
3       c4           2018    6000     3
3       c4           2018    6000     3
3       c4           2018    6000     3
4       c5           2019    6000     3
4       c5           2019    6000     3
4       c5           2019    6000     3

金额除以期限,以便在期限年数之间平均分配金额:

print(s)

  customer  contract_year  amount  term   val
0       c1           2018    3000     3  1000
0       c1           2018    3000     3  1000
0       c1           2018    3000     3  1000
1       c2           2020    1000     1  1000
2       c3           2019    3000     2  1500
2       c3           2019    3000     2  1500
3       c4           2018    6000     3  2000
3       c4           2018    6000     3  2000
3       c4           2018    6000     3  2000
4       c5           2019    6000     3  2000
4       c5           2019    6000     3  2000
4       c5           2019    6000     3  2000

使用cumcount为每个level=0组创建顺序计数器,然后将此计数器添加到contract_year以生成下一个学期年份:

print(s)

  customer  contract_year  amount  term   val  year
0       c1           2018    3000     3  1000  2018
0       c1           2018    3000     3  1000  2019
0       c1           2018    3000     3  1000  2020
1       c2           2020    1000     1  1000  2020
2       c3           2019    3000     2  1500  2019
2       c3           2019    3000     2  1500  2020
3       c4           2018    6000     3  2000  2018
3       c4           2018    6000     3  2000  2019
3       c4           2018    6000     3  2000  2020
4       c5           2019    6000     3  2000  2019
4       c5           2019    6000     3  2000  2020
4       c5           2019    6000     3  2000  2021

使用pivot_table reshape 数据框:

year customer  contract_year  amount  term    2018    2019    2020    2021
0          c1           2018    3000     3  1000.0  1000.0  1000.0     NaN
1          c2           2020    1000     1     NaN     NaN  1000.0     NaN
2          c3           2019    3000     2     NaN  1500.0  1500.0     NaN
3          c4           2018    6000     3  2000.0  2000.0  2000.0     NaN
4          c5           2019    6000     3     NaN  2000.0  2000.0  2000.0

关于python - 根据现有列中的值计算新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65642320/

相关文章:

javascript - 如何在 django 1.6.5 中执行 JavaScript

python - 在 pandas 中填充 DataFrame

python - Pandas 组合连续值

python - 根据一列中与 Pandas 另一列中的引用日期最接近的日期选择行?

python - 为什么我要在 pandas 中复制数据框

python - 从数据库中断中恢复 Celery

python - 翻译伪代码回文

python - Ruby 的 bundler/Perl 的纸箱的 Python 等价物是什么?

python - 使用 pandas 读取 csv 文件,其中列由不同数量的空格和逗号分隔

python - 在 Python 中使用 Pandas 和 numpy 合并抓取的数据时遇到问题