查找Math.min行 [英] find Math.min row

查看:109
本文介绍了查找Math.min行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在csv文件的列中找到了最低值,但是我找不到存储该值的行.有谁可以帮助我吗?无论如何,尽管我已经在Google和网站上搜索了很多天,但我仍然可以自己找到答案.预先感谢.

function getDataPointsFromCSV(csv) {
            var dataPoints = csvLines =   [];   
            var mini, a
            var minIndex = 0
            csvLines = csv.split(/[\r?\n|\r|\n]+/);
            for (var i = 1; i <= csvLines.length; i++)
                if (csvLines[i] > 0) {
                    points = csvLines[i].split(",");
//points instead of csvLines
                    mini = points[4]
                }
            var a = mini                
            for (var i = 1; i <= csvLines.length; i++)
                if (csvLines[i] < mini) {
                    points = csvLines[i].split(",");                    

                    minIndex = i
                    mini = csvLines[i]                                      
// Find lowest value in a column5(=points[4])( but how to find the row that store the lowest value, can someone help me please?)                
                    lowestv = Math.min(price, lowestv)
             }
// example; mini.length          
            for (var i = mini.length; i <= mini.length+10; i++)
                if (csvLines[i].length > 0) {
                    points = csvLines[i].split(",");
                    price = points[4]
            }
                dataPoints.push({x: new Date(),y: parseFloat(minIndex)});   
        return dataPoints;
        }

解决方案

我将其分为两个步骤.首先,我们解析CSV以获取对象数组.然后,我们遍历它们以查找minValminRow(以及我们可能需要的其他任何统计信息)之类的统计信息.虽然这效率比单遍执行的效率略低,但它会导致更简单,更易于维护的代码.

因此,这里有csv2arr,它将您的CSV字符串转换为如下所示的对象:

 {
    date: "31/10/2019",
    open: 9202.457589,
    high: 9383.160892,
    low: 9028.71744,
    close: 9199.584833
}
 

stats,它们采用类似的对象数组来为每一列查找统计信息,例如maxValminRow.如果需要,我们可以增强它以查找其他统计信息,例如meanmedian.其输出如下所示:

 {
    date: { /* ... */ },
    open: { /* ... */ },
    high: { /* ... */ },
    low: { /* ... */ },
    close: {
        minVal: 9199.584833,
        maxVal: 9427.687584,
        minRow: {date: "31/10/2019", open: 9202.457589, high: 9383.160892, low: 9028.71744, close: 9199.584833},
        maxRow: {date: "29/10/2019", open: 9248.440562, high: 9516.181048, low: 9232.648086, close: 9427.687584}
    }
}
 

这是一种方法:

 // Naive, but useful for some data
const csv2arr = (csv) => {
  const [headers, ...rows] = csv .trim () .split ('\n') .map (r => r .split (','))
  return rows .reduce ((a, r) => [
    ... a, 
    Object .assign (... (r .map ((c, i) => ({[headers [i]]: isNaN(c) ? c : Number(c)}))))
  ], [])
}

const stats = (rows) => rows .reduce (
  (stats, row) => Object .assign (
    ... Object .entries (row) .map (([key, val]) => ({[key]: ({
      minVal: key in stats && stats [key] .minVal < val ? stats [key] .minVal : val,
      maxVal: key in stats && stats [key] .maxVal > val ? stats [key] .maxVal : val,
      minRow: key in stats && stats [key] .minRow [key] < val ? stats [key] .minRow : row,
      maxRow: key in stats && stats [key] .maxRow [key] > val ? stats [key] .maxRow : row,
    })}))
  ),
  {}
)

const csv = `
date,open,high,low,close
31/10/2019,9202.457589,9383.160892,9028.71744,9199.584833
30/10/2019,9422.463325,9426.874217,9085.370357,9205.726559
29/10/2019,9248.440562,9516.181048,9232.648086,9427.687584
28/10/2019,9565.101883,9805.118089,9256.148389,9256.148389
`

const rows = csv2arr (csv)
const statistics = stats (rows)

console .log (rows)
console .log (statistics) 

请注意,此处日期的最小值/最大值没有太大意义.如果它们是ISO格式的(例如"2019-10-31"),那么这些值也将获得有意义的最大值和最小值.

csv2arr在相当广泛的情况下很有用.但是不要将其误认为是完整的CSV解析器.例如,如果您的数据包含带有逗号的单元格,它将失败.对输出也很幼稚.本质上,即使它看起来像一个数字,它也会变成一个数字,即使该列中的其他内容不同.还有其他问题.但是它对于许多逗号分隔格式仍然有用.

I have already found the lowest value in a column in a csv file, but I can't find which row store this value. Can someone help me please? Is there anyway I can find the answer by myself though I have googled for many days and websites. Thanks in advance.

function getDataPointsFromCSV(csv) {
            var dataPoints = csvLines =   [];   
            var mini, a
            var minIndex = 0
            csvLines = csv.split(/[\r?\n|\r|\n]+/);
            for (var i = 1; i <= csvLines.length; i++)
                if (csvLines[i] > 0) {
                    points = csvLines[i].split(",");
//points instead of csvLines
                    mini = points[4]
                }
            var a = mini                
            for (var i = 1; i <= csvLines.length; i++)
                if (csvLines[i] < mini) {
                    points = csvLines[i].split(",");                    

                    minIndex = i
                    mini = csvLines[i]                                      
// Find lowest value in a column5(=points[4])( but how to find the row that store the lowest value, can someone help me please?)                
                    lowestv = Math.min(price, lowestv)
             }
// example; mini.length          
            for (var i = mini.length; i <= mini.length+10; i++)
                if (csvLines[i].length > 0) {
                    points = csvLines[i].split(",");
                    price = points[4]
            }
                dataPoints.push({x: new Date(),y: parseFloat(minIndex)});   
        return dataPoints;
        }

解决方案

I would break this into two steps. First, we parse the CSV to get an array of objects. Then we iterate over them to find stats like minVal and minRow (and any others we might want.) While this is slightly less efficient than doing it in a single pass, it leads to simpler, more maintainable code.

So here we have csv2arr, which converts your CSV string into objects that look like this:

{
    date: "31/10/2019",
    open: 9202.457589,
    high: 9383.160892,
    low: 9028.71744,
    close: 9199.584833
}

and stats, which takes an array of objects like that to find statistics such as maxVal and minRow for each column. We could enhance this to find other stats such as mean and median if desired. Its output looks like this:

{
    date: { /* ... */ },
    open: { /* ... */ },
    high: { /* ... */ },
    low: { /* ... */ },
    close: {
        minVal: 9199.584833,
        maxVal: 9427.687584,
        minRow: {date: "31/10/2019", open: 9202.457589, high: 9383.160892, low: 9028.71744, close: 9199.584833},
        maxRow: {date: "29/10/2019", open: 9248.440562, high: 9516.181048, low: 9232.648086, close: 9427.687584}
    }
}

This is one way to do so:

// Naive, but useful for some data
const csv2arr = (csv) => {
  const [headers, ...rows] = csv .trim () .split ('\n') .map (r => r .split (','))
  return rows .reduce ((a, r) => [
    ... a, 
    Object .assign (... (r .map ((c, i) => ({[headers [i]]: isNaN(c) ? c : Number(c)}))))
  ], [])
}

const stats = (rows) => rows .reduce (
  (stats, row) => Object .assign (
    ... Object .entries (row) .map (([key, val]) => ({[key]: ({
      minVal: key in stats && stats [key] .minVal < val ? stats [key] .minVal : val,
      maxVal: key in stats && stats [key] .maxVal > val ? stats [key] .maxVal : val,
      minRow: key in stats && stats [key] .minRow [key] < val ? stats [key] .minRow : row,
      maxRow: key in stats && stats [key] .maxRow [key] > val ? stats [key] .maxRow : row,
    })}))
  ),
  {}
)

const csv = `
date,open,high,low,close
31/10/2019,9202.457589,9383.160892,9028.71744,9199.584833
30/10/2019,9422.463325,9426.874217,9085.370357,9205.726559
29/10/2019,9248.440562,9516.181048,9232.648086,9427.687584
28/10/2019,9565.101883,9805.118089,9256.148389,9256.148389
`

const rows = csv2arr (csv)
const statistics = stats (rows)

console .log (rows)
console .log (statistics)

Note that the min/max values for dates here don't make much sense. If they were ISO-formatted (e.g. "2019-10-31"), then these values would also get meaningful maximums and minimums.

csv2arr is useful for a reasonably wide range of cases. But don't mistake it for a full CSV parser. If your data includes cells with commas, for instance, it will fail. It's also naive about the output. Essentially, if it looks like a number, it becomes a number, even if other things in the column disagree. There are other problems, too. But it's still useful for many comma-delimited formats.

这篇关于查找Math.min行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆