关于功能:Python / Pandas:如何使用从现有数据帧计算的新变量和值创建结果表

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

我希望能够创建一个交叉表/表/数据帧(无论名称是什么),如下所示:

1
2
3
4
5
____________________      
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数据集中不存在performance和ltfu,应该创建这两个数据集,以便我对性能进行总结。

我现在得到的信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)

您可以将上述代码复制并粘贴到您的PythonIDE和测试中,然后将您的解决方案返回给我。请给我一个我想要的带有表结果的例子。


您需要将输出分配给DataFrame,然后由DataFrame.to_csv写入文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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=' ')


您的要求不符合熊猫数据帧的定义,您已经拥有这些值,因此您可以使用其他方式使用输出。