简体   繁体   中英

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:

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:

# [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. So in the end I would like to have 10 columns, each with its belonging columns from the input and the output file. 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. Would that be possible?

Expected 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. It can accept pandas dataframe and numpy array as input.

np.c_[df, res]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM