Python: 如何将水平数据点列表转换为垂直数据框?
Python: How to convert horizontal list of data points into vertical dataframe?
我有一个变量,它是数据点 (x, y) 的水平列表。我如何转换它:
[(6493.16, 3.12), (5575.26, 2.76), (5571.83, 14.73), (7202.88, 3.08), (7569.92, 4.01), (7289.38, 10.24)]
对此:
6493.16, 3.12
5575.26, 2.76
5571.83, 14.73
7202.88, 3.08
7569.92, 4.01
7289.38, 10.24
这样我就可以将所有 y 值存储在一个单独的变量中,然后取所有 y 值的平均值?
只需将其分配给变量然后使用 pandas.DataFrame
import pandas as pd
x = [(6493.16, 3.12), (5575.26, 2.76), (5571.83, 14.73), (7202.88, 3.08), (7569.92, 4.01), (7289.38, 10.24)]
df = pd.DataFrame(x)
您的列表只是一个 6x2 矩阵。
使用 numpy 从矩阵中获取第二列。
import numpy as np
A = np.array([(6493.16, 3.12), (5575.26, 2.76), (5571.83, 14.73), (7202.88, 3.08), (7569.92, 4.01), (7289.38, 10.24)])
#to get n-th column from matrix A, where n=0,1,2,.. use: A[:, n]
#in your case:
y = A[:, 1]
#and you can applay mean on this column
avg = np.mean(y)
我有一个变量,它是数据点 (x, y) 的水平列表。我如何转换它:
[(6493.16, 3.12), (5575.26, 2.76), (5571.83, 14.73), (7202.88, 3.08), (7569.92, 4.01), (7289.38, 10.24)]
对此:
6493.16, 3.12
5575.26, 2.76
5571.83, 14.73
7202.88, 3.08
7569.92, 4.01
7289.38, 10.24
这样我就可以将所有 y 值存储在一个单独的变量中,然后取所有 y 值的平均值?
只需将其分配给变量然后使用 pandas.DataFrame
import pandas as pd
x = [(6493.16, 3.12), (5575.26, 2.76), (5571.83, 14.73), (7202.88, 3.08), (7569.92, 4.01), (7289.38, 10.24)]
df = pd.DataFrame(x)
您的列表只是一个 6x2 矩阵。
使用 numpy 从矩阵中获取第二列。
import numpy as np
A = np.array([(6493.16, 3.12), (5575.26, 2.76), (5571.83, 14.73), (7202.88, 3.08), (7569.92, 4.01), (7289.38, 10.24)])
#to get n-th column from matrix A, where n=0,1,2,.. use: A[:, n]
#in your case:
y = A[:, 1]
#and you can applay mean on this column
avg = np.mean(y)