首页 > 解决方案 > 用 float 为 pandas 转换列表

问题描述

这是我第一次发布问题,我是 python 新手。我试图在下面的公式中将 'list1' 和 'list2' 插入到 'x' 和 'y' 并且出现错误,例如

('无法将 1.16041.16001.16351.17491.17661.17501.17461.17471.1779 转换为数字')

import numpy as np
import pandas as pd
from scipy import stats
from pandas import *

list1 = ['1.1575', '1.1604', '1.1600', '1.1635', '1.1749', '1.1766', '1.1750', '1.1746', '1.1747', '1.1779']
list2 = ['6604.11341382', '6688.01480364', '6668.72146384', '6553.56452794', '6499.18728419', '6629.18122154', '6724.42744078', '6737.98000228', '6755.31691870', '6556.66000350']

# Method 2 (Correlation, p-value)
def pcc(x,y):
    x = x - x.mean(0)
    y = y - y.mean(0)
    x /= x.std(0)
    y /= y.std(0)
    return np.mean(x*y)

x = np.array(x)
y = np.array(y)
print(linregress(x,y))

标签: pythonpython-3.xpandaslistdataframe

解决方案


尝试在 list1 和 2 的初始化下面添加:

list1 = [ float(z) for z in list1 ]
list2 = [ float(z) for z in list2 ]

推荐阅读