Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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文件读取并合并数据_Python_Sorting_Csv_Pandas - Fatal编程技术网

Python 从多个csv文件读取并合并数据

Python 从多个csv文件读取并合并数据,python,sorting,csv,pandas,Python,Sorting,Csv,Pandas,我有3个不同的文件:NewRush4.csv、NewRush5.csv、NewRush6.csv。 我试图从每一季(第四季、第五季和第六季)收集一份“所有时间的领导者” 我想读取每个文件中每个玩家的名字,如果它们是重复的,则将其合并,或者读取第一个文件并将其与其他两个文件进行比较,以合并它们 这是我的python代码。我必须读第一个文件。我不知道如何使用DictReader #!/usr/bin/python import csv file = open("NewRush4.csv", "rb"

我有3个不同的文件:NewRush4.csv、NewRush5.csv、NewRush6.csv。 我试图从每一季(第四季、第五季和第六季)收集一份“所有时间的领导者”

我想读取每个文件中每个玩家的名字,如果它们是重复的,则将其合并,或者读取第一个文件并将其与其他两个文件进行比较,以合并它们

这是我的python代码。我必须读第一个文件。我不知道如何使用DictReader

#!/usr/bin/python
import csv
file = open("NewRush4.csv", "rb")

for line in csv.DictReader(file, delimiter=","):
    name = line["Player"].strip()
    yds = line["YDS"].strip()
    car = line["CAR"].strip()
    td = line["TD"].strip()
    fum = line["FUM"].strip()
    ypc = line["YPC"].strip()

    print "%-20s%10s%10s%10s%10s%10s" % (name, car, yds, td, fum, ypc)
file.close()
输出:

49erswag                     3      14.0         0         0       4.7  
A Beast Playa                7      23.0         0         0       3.3  
A Swanky Guy 2              29     154.0         1         2       5.3  
ACIDRUST                     1       4.0         0         0       4.0  
Aj dahitman                227    1898.0        19         2       8.4  
Aldizzl                     10      45.0         0         0       4.5  
Areis21                     13      58.0         0         2       4.5  
at43                        48     214.0         1         1       4.5  
Ayala2012xTCU               57     195.0         0         1       3.4  
B O R Nx 25                 13      31.0         0         1       2.4  
B r e e z yx60               4      13.0         0         0       3.3  
Beardown74                 116     621.0         6         3       5.4  
beatdown54 2010             26     126.0         3         1       4.8  
behe SWAG                    1      -5.0         0         0      -5.0  
Big Murph22                 73     480.0         6         2       6.6  
BigBlack973                 18      57.0         0         1       3.2  
BiGDaDDyNaPSacK            184    1181.0        20         4       6.4  
第四季档案:

Player,YDS,TD,CAR,FUM,YPC  
49erswag,   14.0,   0,   3,   0,   4.7  
A Beast Playa,   23.0,   0,   7,   0,   3.3  
A Swanky Guy 2,   154.0,   1,   29,   2,   5.3  
ACIDRUST,   4.0,   0,   1,   0,   4.0  
Aj dahitman,   1898.0,   19,   227,   2,   8.4  
Aldizzl,   45.0,   0,   10,   0,   4.5  
Areis21,   58.0,   0,   13,   2,   4.5  
at43,   214.0,   1,   48,   1,   4.5  
Ayala2012xTCU,   195.0,   0,   57,   1,   3.4  
B O R Nx 25,   31.0,   0,   13,   1,   2.4  
B r e e z yx60,   13.0,   0,   4,   0,   3.3  
...  
第五季档案:

Player,YDS,TD,CAR,FUM,YPC  
a toxic taz,   307.0,   4,   44,   0,   7.0  
AbNL Boss,   509.0,   4,   174,   2,   2.9  
AFFISHAUL,   190.0,   0,   35,   2,   5.4  
AJ DA HITMAN,   1283.0,   19,   228,   6,   5.6  
allen5422,   112.0,   2,   18,   0,   6.2  
Allxdayxapx,   264.0,   1,   76,   2,   3.5  
AlpHaaNike,   51.0,   1,   10,   1,   5.1  
Aura Reflexx,   215.0,   1,   40,   0,   5.4  
AWAKEN DA BEAST,   -5.0,   0,   4,   1,   -1.3  
AxDub24,   -3.0,   0,   2,   1,   -1.5  
Ayala2012xTCU,   568.0,   4,   173,   1,   3.3  
BALLxXHAWKXx,   221.0,   1,   47,   2,   4.7   
BANG FIGHTY007,   983.0,   6,   171,   3,   5.7  
bang z ro,   29.0,   0,   9,   0,   3.2  
BEARDOWN74,   567.0,   6,   104,   2,   5.5  
...  

因此,如果一名球员踢了一个以上的赛季,请添加他的统计数据和打印。否则,只需打印即可

您可以尝试使用python pandas,这似乎是您需要的工具。对于阅读部分,您可以使用,然后创建三个
DataFrame
(或者一个,包含所有记录),并进一步对它们进行操作

例如,对于重复项,您可以尝试使用
duplicated
,例如,使用
df[df.duplicated('Player')]
。您还将发现可能需要很多函数,例如
max
。看一看

为了让你尝到味道(基于原始帖子中的第四季和第五季数据):

希望有帮助。

使用:

我不知道每个领域意味着什么;我对每个字段求和。根据需要调整

from collections import defaultdict
import csv

class PlayerStat(object):
    def __init__(self, yds=0, car=0, td=0, fum=0, ypc=0, count=0):
        self.yds   = float(yds)
        self.car   = float(car)
        self.td    = float(td)
        self.fum   = float(fum)
        self.ypc   = float(ypc)
        self.count = count
    def __iadd__(self, other):
        self.yds   += other.yds
        self.car   += other.car
        self.td    += other.td
        self.fum   += other.fum
        self.ypc   += other.ypc
        self.count += other.count
        return self

filenames = 'NewRush4.csv', 'NewRush5.csv', 'NewRush6.csv',
stats = defaultdict(PlayerStat)
for filename in filenames:
    with open(filename) as f:
        reader = csv.DictReader(f, delimiter=',')
        for row in reader:
            stat = PlayerStat(row['YDS'], row['CAR'], row['TD'], row['FUM'], row['YPC'], count=1)
            stats[row['Player']] += stat

for player in sorted(stats, key=lambda player: stats[player].yds):
    stat = stats[player]
    if stat.count == 1:
        continue
    print '{0:<20}{1.car:>10}{1.yds:>10}{1.td:>10}{1.fum:>10}{1.ypc:>10}'.format(player, stat)
从集合导入defaultdict
导入csv
类PlayerStat(对象):
def u u初始值_;(自我,yds=0,car=0,td=0,fum=0,ypc=0,计数=0):
self.yds=浮动(yds)
self.car=浮动(car)
self.td=浮动(td)
self.fum=浮子(fum)
self.ypc=浮动(ypc)
self.count=计数
定义(自我、其他):
self.yds+=其他.yds
self.car+=其他.car
self.td+=其他.td
self.fum+=其他.fum
self.ypc+=其他.ypc
self.count+=其他.count
回归自我
文件名='NewRush4.csv'、'NewRush5.csv'、'NewRush6.csv',
stats=defaultdict(PlayerStat)
对于文件名中的文件名:
打开(文件名)为f时:
reader=csv.DictReader(f,分隔符=',')
对于读取器中的行:
stat=PlayerStat(第['YDS']行、第['CAR']行、第['TD']行、第['FUM']行、第['YPC']行、计数=1)
统计数据[行['Player']]+=统计数据
对于排序中的玩家(stats,key=lambda-player:stats[player].yds):
状态=状态[玩家]
如果stat.count==1:
持续
打印“{0:10}{1.yds:>10}{1.td:>10}{1.fum:>10}{1.ypc:>10}”。格式(玩家,状态)

谢谢你的建议@user2556506我写了一个代码片段,让你对事情有更好的感觉。谢谢!这是有道理的!不知道为什么我没有早点找到熊猫!我刚刚意识到一个文件没有标题。但这工作做得很好!我现在想按YDS排序,这会像stats.sort(lambda X:X[2])一样简单吗?@user2556506,我更新了按YDS排序的代码<代码>排序(stats,key=lambda player:stats[player].yds)谢谢!这很有帮助,我现在可以把它应用到所有事情上了!:)我不能告诉你我花了多长时间!
from collections import defaultdict
import csv

class PlayerStat(object):
    def __init__(self, yds=0, car=0, td=0, fum=0, ypc=0, count=0):
        self.yds   = float(yds)
        self.car   = float(car)
        self.td    = float(td)
        self.fum   = float(fum)
        self.ypc   = float(ypc)
        self.count = count
    def __iadd__(self, other):
        self.yds   += other.yds
        self.car   += other.car
        self.td    += other.td
        self.fum   += other.fum
        self.ypc   += other.ypc
        self.count += other.count
        return self

filenames = 'NewRush4.csv', 'NewRush5.csv', 'NewRush6.csv',
stats = defaultdict(PlayerStat)
for filename in filenames:
    with open(filename) as f:
        reader = csv.DictReader(f, delimiter=',')
        for row in reader:
            stat = PlayerStat(row['YDS'], row['CAR'], row['TD'], row['FUM'], row['YPC'], count=1)
            stats[row['Player']] += stat

for player in sorted(stats, key=lambda player: stats[player].yds):
    stat = stats[player]
    if stat.count == 1:
        continue
    print '{0:<20}{1.car:>10}{1.yds:>10}{1.td:>10}{1.fum:>10}{1.ypc:>10}'.format(player, stat)