Python/Pandas:如何使用根据现有数据框计算的新变量和值创建 table 结果

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

我希望能够像这样创建一个十字架 table/table/dataframe(随便叫什么名字):

____________________      
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)
____________________

请注意,性能和 LTFU 值是从应用于 python 中的 .csv 数据集的函数生成的。 .csv 数据集中不存在性能和 LTFU,应该创建两者只是为了让我对性能进行总结。

我得到的结果如下:

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)

您可以将以上代码复制并粘贴到您的 python IDE 并进行测试,然后 return 将您的解决方案发给我。 请给我一个 table 结果的例子。

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

您需要将输出分配给 DataFrame,然后按 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=' ')