[英]Print each row from input file to output file in python
I have following input data frame:我有以下输入数据框:
df = pd.read_csv('path/testfile.csv', delimiter='\t', header=None)
print(df)
0 1 2 3 4 5
0 0.227996 0.337029 0.238164 0.183009 0.085747 0.134129
1 0.247891 0.335556 0.272129 0.187329 0.085921 0.128372
2 0.264761 0.337778 0.245918 0.183212 0.080493 0.122786
3 0.305061 0.337778 0.204265 0.208453 0.071558 0.083683
4 0.222749 0.337029 0.209715 0.084253 0.142014 0.234673
5 0.190816 0.337029 0.291872 0.041575 0.463764 0.053193
6 0.299625 0.337029 0.206064 0.200905 0.072955 0.092528
7 0.259740 0.340045 0.202792 0.156021 0.087506 0.148796
With this input data frame, I would like to get a new table with following code in python:有了这个输入数据框,我想在 python 中得到一个带有以下代码的新表:
def f2_bis(z, Ai, Bi, Ci, Di, Ei, Fi, Hi):
a, b, c, d = z
return np.array([
Ai*a*((1-c)*Bi+(b-d)*Ci)-Di,
Ei+a*c*d*b,
Fi+Hi*c*a*d+b,
Hi+Fi+Ei*a*b*c*d
])
# unpack the value of each column in different variable
A, B, C ,D, E, F = df.to_numpy().T
H = 1-A
# get the result for each "row"
res = [
fsolve(f2_bis, [1,1,1,1], args=(Ai, Bi, Ci, Di, Ei, Fi, Hi))
for Ai, Bi, Ci, Di, Ei, Fi, Hi in zip(A, B, C, D, E, F, H)
]
res
The output table at the moment looks like this:目前的 output 表如下所示:
# [array([ 1.25482583, -0.32608114, -0.17861197, -0.98296457]),
# array([ 0.70750447, -0.41512857, -0.30218114, -1.80533338]),
# array([-2.91283478, -0.41076736, 1.41022472, -0.09615889]),
# array([ 2.87736785, 0.25256582, 0.6107988 , -0.26507222]),
# array([-0.532438 , 0.34016552, 6.64918304, 0.18908195]),
# array([ 1.1062844 , 0.73110855, -0.65958519, 1.32070547]),
# array([1., 1., 1., 1.]),
# array([1., 1., 1., 1.])]
But the problem is now, that I would like to print the input columns for each row as the first 6 columns and then print the output table with the additional 4 columns.但现在的问题是,我想将每行的输入列打印为前 6 列,然后打印 output 表和额外的 4 列。 So in the end I would like to have 10 columns, each with its belonging columns from the input and the output file.所以最后我想要 10 列,每列都有来自输入和 output 文件的所属列。 My aim is just, to write a function that includes already the columns from the input file and just adds the column from the output.我的目标只是编写一个 function,其中已经包含输入文件中的列,并添加来自 output 的列。 Would that be possible?那可能吗?
Expected Output:预期 Output:
# [array([ 0.227996, 0.337029, 0.238164,0.183009, 0.085747, 0.134129, 1.25482583, -0.32608114, -0.17861197, -0.98296457]),
# array([ 0.247891, 0.335556, 0.272129, 0.187329, 0.085921, 0.128372, 0.70750447, -0.41512857, -0.30218114, -1.80533338]),
# array([0.264761, 0.337778, 0.245918, 0.183212, 0.080493, 0.122786, -2.91283478, -0.41076736, 1.41022472, -0.09615889]),
# array([0.305061, 0.337778, 0.204265, 0.208453, 0.071558, 0.083683, 2.87736785, 0.25256582, 0.6107988 , -0.26507222]),
# array([0.222749, 0.337029, 0.209715, 0.084253, 0.142014, 0.234673, -0.532438 , 0.34016552, 6.64918304, 0.18908195]),
# array([0.190816, 0.337029, 0.291872, 0.041575, 0.463764, 0.053193, 1.1062844 , 0.73110855, -0.65958519, 1.32070547]),
# array([0.299625, 0.337029, 0.206064, 0.200905, 0.072955, 0.092528, 1., 1., 1., 1.]),
# array([0.259740, 0.340045, 0.202792, 0.156021, 0.087506, 0.148796, 1., 1., 1., 1.])]
Numpy has a function that do that. Numpy 有一个 function 可以做到这一点。 It can accept pandas dataframe and numpy array as input.它可以接受 pandas dataframe 和 numpy 数组作为输入。
np.c_[df, res]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.