【问题标题】:Python Google Sheets APIPython 谷歌表格 API
【发布时间】:2020-10-18 15:41:36
【问题描述】:

所以我有这个谷歌表格 API,我正在从中获取数据并运行 KS 测试。但是,我只想对一个数字运行 KS 测试。但是,字符串也由单词组成。例如,给你

 2020-09-15 00:05:13,chemsense,co,concentration,-0.51058,
2020-09-15 00:05:43,chemsense,co,concentration,-0.75889,
2020-09-15 00:06:09,chemsense,co,concentration,-1.23385,
2020-09-15 00:06:33,chemsense,co,concentration,-1.23191,
2020-09-15 00:06:58,chemsense,co,concentration,-0.94495,
2020-09-15 00:07:23,chemsense,co,concentration,-1.16024,

如果我将其作为字符串,我将如何仅对每行的最后一个数字运行 KS 测试。对于实例,我只想在 -.51,-.75,-1.23,-1.23,-.94,-1.16 上运行 KS 测试

这是我的 Google 表格的屏幕截图:

这是我的一些代码:

from scipy import stats
import numpy as np
import gspread
from oauth2client.service_account import  ServiceAccountCredentials
import re


np.seterr(divide='ignore', invalid='ignore')
def estimate_cdf (col,bins=10,):
    print (col)
    # 'col'
    # 'bins'

    hist, edges = np.histogram(col)
    csum = np.cumsum(hist)



    return csum/csum[-1], edges
    print (csum)



scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)

client = gspread.authorize(creds)

sheet = client.open("sheet1").sheet1  # Opens the spreadhseet

data = sheet.get_all_records()


row = sheet.row_values(3)  # Grab a specific row






number_regex = r'^-?\d+\.?\d*$'





col = sheet.col_values(3)  # Get a specific column print (col)

col2= sheet.col_values(4)
dolphin= estimate_cdf(adjusted := [float(i) for i in col if re.match(i, number_regex)], len(adjusted))



print(col)
print(col2)




shtest =stats.shapiro(col)
print(shtest)




#thelight= sheet.update_cell(5,6,col)
#print(thelight)

k2test =stats.ks_2samp(col, col2, alternative='two-sided', mode='auto')
print(k2test)

这是我的一些错误信息:

温度,64.79599999999999,65.03830769230765', '2020-09-25 11:38:51,metsense,htu21d,温度,64.85,65.01338461538458', '2020-09-25 11:39:16,htudense, ,64.994,64.99538461538458', '2020-09-25 11:39:42,metsense,htu21d,温度,65.066,64.98015384615381', '2020-09-25 11:40:06,metsense,htu291d,994967,99499.99 ', '2020-09-25 11:40:31,metsense,htu21d,温度,64.976,64.93861538461535', '2020-09-25 11:40:57,metsense,htu21d,温度,65.066,64.93307692307688', '2 -09-25 11:41:22,metsense,htu21d,温度,65.048,64.93584615384611', '2020-09-25 11:41:48,metsense,htu21d,温度,64.994,64.92753846153843', '2020-09-25 11:42:12,metsense,htu21d,温度,64.976,64.93169230769227', '2020-09-25 11:42:37,metsense,htu21d,温度,64.94,64.9441538461538', '2020-09-25 11:43: 03,metsense,htu21d,温度,64.994,64.95523076923072', '2020-09-25 11:43:28,metsense,htu21d,温度,64.9'] 回溯(最近一次通话最后): 文件“C:/Users/james/PycharmProjectsfreshproj/shapiro wilks.py”,第 60 行,在 shtest =stats.shapiro(col) 文件“C:\Users\james\PycharmProjectsfreshproj\venv\lib\site-packages\scipy\stats\morestats.py”,第 1676 行,在 shapiro a, w, pw, ifault = statlib.swilk(y, a[:N//2], init) ValueError:无法将字符串转换为浮点数:',,,,,'

进程以退出代码 1 结束

【问题讨论】:

    标签: python google-sheets data-science


    【解决方案1】:

    问题

    给定来自 Google Sheets API 的字符串,对每个字符串的最后一个数字运行 kstest。

    解决方案

    更好的方法是直接从 Google Sheets API 获取数字,存储它们并提供给 stats.kstest

    使用现有字符串

    您可以使用str.split 拆分字符串,然后将其转换为浮动。

    示例

    >>> s = '2020-09-15 00:05:43,chemsense,co,concentration,-0.75889,'
    
    >>> s.split(',')
    ['2020-09-15 00:05:43', 'chemsense', 'co', 'concentration', '-0.75889', '']
    
    >>> s.split(',')[4] # get the number (5th item in the list)
    '-0.75889'
    
    >>> float(s.split(',')[4]) # convert to float type
    -0.75889
    
    >>> round(float(s.split(',')[4]), 2) # round to 2 decimal place
    -0.76
    
    from scipy import stats
    
    # Assuming strings coming back from API are in a list
    str = [
    '2020-09-15 00:05:13,chemsense,co,concentration,-0.51058,',
    '2020-09-15 00:05:43,chemsense,co,concentration,-0.75889,',
    '2020-09-15 00:06:09,chemsense,co,concentration,-1.23385,',
    '2020-09-15 00:06:33,chemsense,co,concentration,-1.23191,',
    '2020-09-15 00:06:58,chemsense,co,concentration,-0.94495,',
    '2020-09-15 00:07:23,chemsense,co,concentration,-1.16024,'
    ]
    
    x = []
    
    for s in str:
      x.append(float(s.split(',')[4]))
    
    stats.kstest(x, 'norm')
    

    【讨论】:

    • 谢谢!然而有一件事,字符串可以很好地改变,所以有没有办法做到这一点,它适用于谷歌 api 中的任何字符串?
    • @JamesWatterton 这真的取决于字符串的样子。就像我说的,更好的方法是直接从 Google Sheets API 获取数字。 '2020-09-15 00:05:13,chemsense,co,concentration,-0.51058,' 目前都在一个谷歌表格单元格中吗?
    • 是的,所有这些信息都在一个单元格中。基本上它是每个单元格一行,所以 2020-09-15 00:05:13,chemsense,co,concentration,-0.51058 都在一个单元格中
    • 说实话,如果可能的话,我更喜欢你的方式。
    • @JamesWatterton 您的后续问题不够具体。输入会是什么样子,输出会是什么?你得到的字符串总是用逗号分隔吗?等等。如果不具体说明问题,我就无法提出任何普遍适用的建议
    猜你喜欢
    • 2017-06-11
    • 2017-11-28
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    相关资源
    最近更新 更多