使用其中一个数据框作为键

时间:2018-01-07 08:05:47

标签: python loops dictionary dataframe ranking

我有3个数据框,包含每日数据:唯一代码,名称,分数。第1行中的第一个值称为Rank,然后我有日期,Rank下的第一列包含排名编号(第一列用作索引)。

**df1** UNIQUE CODES

Rank 12/8/2017 12/9/2017 .... 1/3/2018
1     Code_1      Code_3       Code_4
2     Code_2      Code_1       Code_2
...
1000  Code_5      Code_6       Code_7

**df2** NAMES

Rank 12/8/2017 12/9/2017 .... 1/3/2018
1     Jon        Maria         Peter
2     Brian      Jon           Maria
...
1000  Chris      Tim           Charles

**df3** SCORES

Rank 12/8/2017 12/9/2017 .... 1/3/2018
1     10           20           30
2     15           10           40
...
1000  25           15           20

期望的输出:

我想将这些数据帧组合成一个字典,使用df1代码作为键,所以它看起来像这样:

dictionary = {'Code_1':[Jon, 20] , 'Code_2':[Brian, 15]}

由于有重复的竞争对手,我需要在所有数据系列中总结他们的分数。因此,在上面的示例中,Jon的Score_1将包含12/8/2017和12/9/2017的分数。

有1000行和26列+索引,因此需要一种方法来捕获它们。我认为嵌套循环可以在这里工作,但没有足够的经验来构建一个有效的循环。

最后,我想按最高分数对字典进行排序。请为这些或更直接的方法建议任何解决方案,以结合这些数据并获得分数排名。

我附上了包含姓名,代码和分数的数据框图片。

names

codes

scores

我在下面的3个数据帧中使用了建议的解决方案。请注意,hashtags代表代码,名字的玩家和分数的奖杯:

# reshape to get dates into rows
hashtags_reshaped = pd.melt(hashtags, id_vars = ['Rank'], 
                   value_vars = hashtags.columns, 
                   var_name = 'Date', 
                   value_name = 'Code').drop('Rank', axis = 1)

# reshape to get dates into rows
players_reshaped = pd.melt(players, id_vars = ['Rank'], 
                   value_vars = hashtags.columns, 
                   var_name = 'Date', 
                   value_name = 'Name').drop('Rank', axis = 1)

# reshape to get the dates into rows
trophies_reshaped = pd.melt(trophies, id_vars = ['Rank'], 
                   value_vars = hashtags.columns, 
                   var_name = 'Date', 
                   value_name = 'Score').drop('Rank', axis = 1)

# merge the three together. 
# This _assumes_ that the dfs are all in the same order and that all the data matches up.
merged_df = pd.DataFrame([hashtags_reshaped['Date'], 
hashtags_reshaped['Code'], players_reshaped['Name'], 
trophies_reshaped['Score']]).T
print(merged_df)

# group by code, name, and date; sum the scores together if multiple exist for a given code-name-date grouping
grouped_df = merged_df.groupby(['Code', 'Name', 'Date']).sum().sort_values('Score', ascending = False)
print(grouped_df)

summed_df = merged_df.drop('Date', axis = 1) \
.groupby(['Code', 'Name']).sum() \
.sort_values('Score', ascending = False).reset_index()
summed_df['li'] = list(zip(summed_df.Name, summed_df.Score))
print(summed_df)

但是我得到一个奇怪的输出:总计得分应该是几百或几千(平均得分是200-300,平均参与频率是4-6倍)。我得到的得分结果很接近,但他们的匹配代码和名称正确。

summed_df:

0       (MandiBralaX, 996871590076253)  
1              (Arso_C, 9955130513430)  
2               (ThatRainbowGuy, 9946)  
3                         (fabi, 9940)  
4                      (Dogão, 991917)  
5                      (Hierbo, 99168)  
6               (Clyde, 9916156180128)  
7      (.A.R.M.I.N., 9916014310187143)  
8                (keftedokofths, 9900)  
9                   (⚽AngelSosa⚽, 990)  
10                       (Totoo98, 99)  

group_df:

          Code             Name             Score  \
0       #JL2J02LY      MandiBralaX   996871590076253   
1       #80JQ90VC           Arso_C     9955130513430   
2       #9GGC2CUQ   ThatRainbowGuy              9946   
3       #8LL989QV             fabi              9940   
4        #9PPC89L            Dogão            991917   
5      #2JPLQ8JP8           Hierbo             99168

1 个答案:

答案 0 :(得分:1)

这应该可以帮到你。我没有像你指定的那样在最后创建一个字典;虽然您可能需要这种格式,但您最终会得到嵌套的词典或列表,因为每个代码都有1个名称,但可能有很多与之关联的日期和分数。你怎么想要那些记录 - 列表,字典等?

以下代码返回分组的数据框;您可以将其直接输出到dict(显示),但您可能希望详细指定格式,尤其是在需要有序字典时。 (字典本身没有被排序;如果你真的需要有序字典,你必须from collections import OrderedDict并查看该文档。

import pandas as pd

#create the dfs; note that 'Code' is set up as a string
df1 = pd.DataFrame({'Rank': [1, 2], '12/8/2017': ['1', '2'], '12/9/2017': ['3', '1']})
df1.set_index('Rank', inplace = True)

# reshape to get dates into rows
df1_reshaped = pd.melt(df1, id_vars = ['Rank'], 
                       value_vars = df1.columns, 
                       var_name = 'Date', 
                       value_name = 'Code').drop('Rank', axis = 1)
#print(df1_reshaped)

# create the second df
df2 = pd.DataFrame({'Rank': [1, 2], '12/8/2017': ['Name_1', 'Name_2'], '12/9/2017': ['Name_3', 'Name_1']})
df2.set_index('Rank', inplace = True)

# reshape to get dates into rows
df2_reshaped = pd.melt(df2, id_vars = ['Rank'], 
                       value_vars = df1.columns, 
                       var_name = 'Date', 
                       value_name = 'Name').drop('Rank', axis = 1)
#print(df2_reshaped)

# create the third df
df3 = pd.DataFrame({'Rank': [1, 2], '12/8/2017': ['10', '20'], '12/9/2017': ['30', '10']})
df3.set_index('Rank', inplace = True)

# reshape to get the dates into rows
df3_reshaped = pd.melt(df3, id_vars = ['Rank'], 
                       value_vars = df1.columns, 
                       var_name = 'Date', 
                       value_name = 'Score').drop('Rank', axis = 1)
#print(df3_reshaped)

# merge the three together. 
# This _assumes_ that the dfs are all in the same order and that all the data matches up.
merged_df = pd.DataFrame([df1_reshaped['Date'], df1_reshaped['Code'], df2_reshaped['Name'], df3_reshaped['Score']]).T
print(merged_df)

# group by code, name, and date; sum the scores together if multiple exist for a given code-name-date grouping
grouped_df = merged_df.groupby(['Code', 'Name', 'Date']).sum().sort_values('Score', ascending = False)
print(grouped_df)

summed_df = merged_df.drop('Date', axis = 1) \
    .groupby(['Code', 'Name']).sum() \
    .sort_values('Score', ascending = False).reset_index()
summed_df['li'] = list(zip(summed_df.Name, summed_df.Score))
print(summed_df)

未排序的词典:

d = dict(zip(summed_df.Code, summed_df.li))
print(d)

当然,您可以直接制作OrderedDict,并且应该:

from collections import OrderedDict
d2 = OrderedDict(zip(summed_df.Code, summed_df.li))
print(d2)

summed_df

  Code    Name  Score            li
0    3  Name_3     30  (Name_3, 30)
1    1  Name_1     20  (Name_1, 20)
2    2  Name_2     20  (Name_2, 20)

d

{'3': ('Name_3', 30), '1': ('Name_1', 20), '2': ('Name_2', 20)}

d2,排序:

OrderedDict([('3', ('Name_3', 30)), ('1', ('Name_1', 20)), ('2', ('Name_2', 20))])

这会将你的(姓名,分数)作为元组返回,而不是列表,但......它应该会有更多的方式。