简体   繁体   English

我需要在不使用numpy和pandas的情况下在csv文件中计算第一和第三四分位数

[英]I need to calculate 1st and 3rd quartile in csv file without using numpy and pandas

File looks like 3 columns of integer numbers. 文件看起来像3列整数。 This is what I have But it is returning me TypeError: slice indices must be integers or None or have an index method 这就是我所拥有的,但是它返回给我TypeError:切片索引必须为整数或None或具有索引方法

import statistics

file = open("PokemonF.csv","r")
first_col=[]
sec_col=[]
third_col=[]
columns=[first_col]
for line in file :
    data = line.split(',')
    first_col.append(int(data[0]))
    sec_col.append(int(data[1]))
    third_col.append(int(data[2]))
for col_ in columns:
    min_col = min(first_col)
    sortedData = sorted(first_col)
    mid = len(first_col)/2
    if len(sortedData) % 2 == 0:
        lowerQ = statistics.median(sortedData[:mid])
        upperQ = statistics.median(sortedData[mid:])
    else:  
        lowerQ = statistics.median(sortedData[:mid])
        upperQ = statistics.median(sortedData[mid+1:])
    median_col = statistics.median(first_col)
    max_col = max(first_col)


    print("Minimum: {}".format(min_col))
    print("First quartile: {}".format(lowerQ))
    print("Median: {}".format(median_col))
    print("Maximum: {}".format(max_col))
file.close

I can't test the solution since you failed to provide a Minimal, complete, verifiable example , but I think I see your problem: 由于您未能提供一个最小,完整,可验证的示例 ,因此我无法测试该解决方案,但我认为我看到了您的问题:

mid = len(first_col)/2
if ...
    lowerQ = statistics.median(sortedData[:mid])
    upperQ = statistics.median(sortedData[mid:])

If first_col has an odd quantity of elements, then mid is a float value, such as 7.5. 如果first_col的元素数量为奇数,则mid为浮点值,例如7.5。 You can't use that as a string slice index. 您不能将其用作字符串切片索引。 Try integer division instead: 尝试使用整数除法:

mid = len(first_col) // 2

Depending on your local definition of "quartile" boundary, you may need to add 1. 根据您对“四分位数”边界的本地定义,您可能需要添加1。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在pandas数据帧中过滤属于特定列的第1和第3四分位数的行? - How to filter rows that fall within 1st and 3rd quartile of a particular column in pandas dataframe? 如何在没有模块的情况下获得排序数字列表的第一和第三四分位数 - How to get the 1st and 3rd quartile of a list of sorted numbers without modules 如何计算第一和第三四分位数? - How to calculate 1st and 3rd quartiles? 如何在不导入的情况下计算列表的第 1 个和第 3 个四分位数? - How to calculate the 1st and 3th quartile of a list without import? Pyspark:需要连接多个数据帧,即第一个语句的输出应与第三个数据帧连接,依此类推 - Pyspark : Need to join multple dataframes i.e output of 1st statement should then be joined with the 3rd dataframse and so on 如何在python numpy的二维数组中将位置(第2、第3或第4等)转换为索引(第1位置为00,第2位置为01等)? - how can I convert position (2nd, 3rd or 4th etc ) to index(00 for 1st position, 01 for 2nd etc) in 2D array in python numpy? 使用第一个元素的值获取结果集中第二个和第三个元素的值 - get values of 2nd and 3rd element of a result set using the value of the 1st element python:日期必须是当月的第一周和第三周 - python: date has to be 1st and 3rd week of the month 遍历字典的第 1 + 3 级 - Loop through 1st + 3rd level of a dictionary 使用矢量化(更好)或其他方法比较多个熊猫列(第一和第二,第三和第四之后,等等) - Compare multiple pandas columns (1st and 2nd, after 3rd and 4rth, after etc) with vectorization (better) or other method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM