以下Python中的Matirx向量运算范例产生奇怪的输出错误

以下Python中的Matirx向量运算范例产生奇怪的输出错误

本文介绍了以下Python中的Matirx向量运算范例产生奇怪的输出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在python中执行此操作,这是一个小示例:

I want to do this in python, here is a small example:

number_of_payments = [
    [0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
    [0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
    [1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]
NDD_month = [8, 7, 11]
dates = []
for i in range(len(number_of_payments)):
    dates.append([NDD_month[i]])
    for j in range(1, len(number_of_payments[i])):
        dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates)

这给了我

[[8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8], [7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 4], [11, 10, 7, 6, 6, 6, 5, 4, 3, 2, 2, 1]]

现在,我尝试执行相同的操作,但是要处理整个数据集,但这就是我得到的(我将在下面粘贴我的整个代码):

Now I try to do the same thing but with the entire set of data but this is what I get (I will paste my whole code below):

# Import modules
import numpy as np
import pandas as pd
import datetime

# Import data file
df = pd.read_csv("Paystring Data.csv")
df.head()

# Get column data into a list
x = list(df)

# Append column data into cpi, NDD, and as of dates
NDD = df['NDD 8/31']
cpi = df['Contractual PI']
as_of_date = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))
as_of_date = as_of_date[1:13]
NDD_month = pd.to_datetime(NDD, errors = 'coerce').dt.month.tolist()
# print(as_of_date.dt.month)

# Get cash flows
cf = df.iloc[:,1:13].replace('[^0-9.]', '', regex=True).astype(float)
cf = cf.values

# Calculate number of payments
number_of_payments = []
for i in range(len(cpi)):
    number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))
np.vstack(number_of_payments).tolist()

# Calculate the new NDD dates
dates = []
for i in range(len(number_of_payments)):
    dates.append([NDD_month[i]])
    for j in range(1, len(number_of_payments[i])):
        dates[i].append((dates[i][j-1] + 12 - number_of_payments[i][j-1]) % 12)
print(dates[0])

这只是给我[8]

应为[8, 8, 7, 7, 6, 5, 4, 4, 11, 10, 10, 8].

有人知道如何解决这个问题吗?

Anyone know how to fix this?

推荐答案

在您的小例子"中,number_of_paymentslistlistlist:

In your "small example", number_of_payments is a list of list of ints:

number_of_payments = [
    [0, 1, 0, 1, 1, 1, 0, 5, 1, 0, 2, 1],
    [0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0],
    [1, 3, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0]
]

在您的真实代码中,number_of_paymentsintlist:

In your real code, number_of_payments is a list of ints:

number_of_payments = []
for i in range(len(cpi)):
    number_of_payments.append((cf[:i + 1] / cpi[i]).astype(int))

似乎您需要弄清楚如何通过嵌套使真实的number_of_payments看起来像您的样本.

It seems like you need to figure out how to make your real number_of_payments look like your sample one through nesting.

这篇关于以下Python中的Matirx向量运算范例产生奇怪的输出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 12:49