简体   繁体   中英

Python/Pandas: How to create a table of results with new variables and values calculated from an existing dataframe

I want to be able to create a cross table/table/dataframe (what ever the name) like this:

____________________      
Performance  "value" (This value must come from a X vector, which has a formula to go to dataset, calculate and return this value)
____________________
LTFU         "value" (This value must come from a y vector, which has a formula to go to dataset, calculate and return this value)
____________________

Please, note that Performance and LTFU values are generated from a function applied to a .csv dataset in python. Performance and LTFU don't exist in the .csv dataset, both should be created just to allow me do a summary of performance.

What I get now is as below:

import pandas as pd
performance=pd.read_csv("https://www.dropbox.com/s/08kuxi50d0xqnfc/demo.csv?dl=1")

x=performance["idade"].sum()
y=performance["idade"].mean()

l = "Performance"
k = "LTFU"

def test(y):
return pd.DataFrame({'a':y, 'b':x})

test([l,k])

         a        b
0   Performance   x vector value here (it shows 1300, it is correct)
1   LTFU          y vector value here (it shows 1300, it is wrong, it should show 14.130434782608695 instead, according to the instruction of y vector)

You can copy and paste the above code to your python IDE and test and then return with your solution to me. Please, show me an example with the table results as I want.

您的要求不符合pandas数据框的定义,您已经有了值,因此可能可以通过其他方式使用输出

You need assign output to DataFrame and then write to file by DataFrame.to_csv :

l = "Performance"
k = "LTFU"

#changed input to 2 scalar values
def test(l1,k1):
    #changed a to list [l1, k1]
    #changed b to list [x, y]
    return pd.DataFrame({'a':[l1, k1], 'b':[x, y]})

df1 = test(l,k)
print (df1)
             a            b
0  Performance  1300.000000
1         LTFU    14.130435
df1.to_csv('file.csv', index=False, header=None, sep=' ')

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