Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何计算给定csv/psv混合文本文件中的不同字段?_Python_Dataframe_Pyspark_Rdd - Fatal编程技术网

Python 如何计算给定csv/psv混合文本文件中的不同字段?

Python 如何计算给定csv/psv混合文本文件中的不同字段?,python,dataframe,pyspark,rdd,Python,Dataframe,Pyspark,Rdd,我相信Python是最好的选择,但我可能错了 以下是Linux中文本格式的数据源示例: TUI,39832020:09:01,10.56| TUI,39832020:10:53,11.23| TUI,39832020:15:40,23.20 DIAN,39832020:09:04,11.56| TUI,39832020:11:45,11.23| DIAN,39832020:12:30,23.20| SLD,39832020:11:45,11.22 大小未知,假设有一百万行 每行包含三个或更多由

我相信Python是最好的选择,但我可能错了

以下是Linux中文本格式的数据源示例:

TUI,39832020:09:01,10.56| TUI,39832020:10:53,11.23| TUI,39832020:15:40,23.20
DIAN,39832020:09:04,11.56| TUI,39832020:11:45,11.23| DIAN,39832020:12:30,23.20| SLD,39832020:11:45,11.22
大小未知,假设有一百万行

每行包含三个或更多由
|
分隔的集合,每个集合都有由
分隔的字段

每组中的第一个字段是产品ID。例如,在上面的示例中,
TUI
DIAN
SLD
是产品ID

我需要知道我有多少种产品存档。例如,第一行包含1:
TUI
,第二行包含3:
DIAN
TUI
,和
SLD

总之,在这两条线上,我们可以看到有三种独特的产品

有人能帮忙吗

多谢各位。任何有启发性的建议都值得赞赏

更新

我更喜欢一个基于Python和Spark的解决方案,即pySpark

我也在寻找统计数据,如:

  • 每种产品的总量
  • 给定时间的所有记录(每组中的第二个字段,如
    39832020:09:01
  • 每种产品的最低和最高价格
更新2

谢谢你们的代码,我真的很感激。我想知道是否有人可以将数据写入RDD和/或数据帧。我知道在SparkSQL中,获取这些统计信息非常简单

先谢谢你


非常感谢。

类似于Accdias的回答:使用字典,逐行阅读文件,按
然后按
拆分数据,并将字典中的计数相加

myFile="lines_to_read.txt"
productCounts = dict()

with open(myFile, 'r') as linesToRead:
    for thisLine in linesToRead:
        for myItem in thisLine.split("|"):
            productCode=myItem.split(",")
            productCode=productCode[0].strip()
            if productCode in productCounts:
                productCounts[productCode]+=1
            else:
                productCounts[productCode]=1

print(productCounts)
****更新**** Dataframe与Pandas一起使用,以便我们可以查询数据后缀的统计信息:

import pandas as pd

myFile="lines_to_read.txt"
myData = pd.DataFrame (columns=['prodID', 'timeStamp', 'prodPrice'])

with open(myFile, 'r') as linesToRead:
    for thisLine in linesToRead:
        for myItem in thisLine.split("|"):
            thisItem=myItem.strip('\n, " "').split(",")
            myData = myData.append({'prodID':thisItem[0],'timeStamp':thisItem[1],'prodPrice':thisItem[2]}, ignore_index=True)

print(myData)   # Full Table
print(myData.groupby('prodID').agg({'prodID':'count'}))  # Total of prodID's
print(myData.loc[myData['timeStamp'] == '39832020:11:45']) # all lines where time = 39832020:11:45
print(myData.groupby('prodID').agg({'prodPrice':['min', 'max']})) # min/max prices

与Accdias的回答类似:使用字典,逐行读取文件,按
拆分数据,然后按
将字典中的计数相加

myFile="lines_to_read.txt"
productCounts = dict()

with open(myFile, 'r') as linesToRead:
    for thisLine in linesToRead:
        for myItem in thisLine.split("|"):
            productCode=myItem.split(",")
            productCode=productCode[0].strip()
            if productCode in productCounts:
                productCounts[productCode]+=1
            else:
                productCounts[productCode]=1

print(productCounts)
****更新**** Dataframe与Pandas一起使用,以便我们可以查询数据后缀的统计信息:

import pandas as pd

myFile="lines_to_read.txt"
myData = pd.DataFrame (columns=['prodID', 'timeStamp', 'prodPrice'])

with open(myFile, 'r') as linesToRead:
    for thisLine in linesToRead:
        for myItem in thisLine.split("|"):
            thisItem=myItem.strip('\n, " "').split(",")
            myData = myData.append({'prodID':thisItem[0],'timeStamp':thisItem[1],'prodPrice':thisItem[2]}, ignore_index=True)

print(myData)   # Full Table
print(myData.groupby('prodID').agg({'prodID':'count'}))  # Total of prodID's
print(myData.loc[myData['timeStamp'] == '39832020:11:45']) # all lines where time = 39832020:11:45
print(myData.groupby('prodID').agg({'prodPrice':['min', 'max']})) # min/max prices

我想我明白了你说的一切,并为每项任务更新了我的答案。如果您还需要什么,请查看并告诉我。
39832020:09:01
中的
39832020
是什么?第二部分我可以推断现在是时候了,但我无法计算出前半部分。你已经尝试了什么?请检查一下。通过遵循这些文章的提示,您将获得更好的结果。尽管我已经为您提供了另一种选择,但您需要向我们展示您自己所做的事情,然后我们可以帮助您走得更远。我们无法为您编写解决方案。我想我已经把你说的都弄明白了,并且用每个任务的片段更新了我的答案。如果您还需要什么,请查看并告诉我。
39832020:09:01
中的
39832020
是什么?第二部分我可以推断现在是时候了,但我无法计算出前半部分。你已经尝试了什么?请检查一下。通过遵循这些文章的提示,您将获得更好的结果。尽管我已经为您提供了另一种选择,但您需要向我们展示您自己所做的事情,然后我们可以帮助您走得更远。我们无法为您编写解决方案。所以不是为了这种事情。谢谢Brian,有可能提供RDD/Dataframe解决方案吗?@mdivk挑战已接受,答案已更新。注意:这段代码是非常分条的,很少或没有错误处理,并且基于一个很小的数据集。谢谢Brian,我要求DataFrame解决方案的原因是我喜欢通过将df注册为诱惑来进行特殊查询,我知道pandas数据框中有API可以做到这一点,我个人认为诱惑是快速完成任务的最简单方法谢谢Brian,有可能提供RDD/Dataframe解决方案吗?@mdivk挑战已接受,答案已更新。注意:这段代码是非常分条的,很少或没有错误处理,并且基于一个很小的数据集。谢谢Brian,我要求DataFrame解决方案的原因是我喜欢通过将df注册为诱惑来进行特殊查询,我知道pandas数据框中有API可以做到这一点,我个人认为诱惑是快速完成任务的最简单方法