熊猫迭代两个数据框

时间:2020-01-13 18:30:12

标签: python pandas

我有两个df,一个是我的几个ID的列表,另一个是人名和ID。

我想让他们循环,当df1中的ID等于df2 ID时,他在df2中取名字并在df1中创建。

我试图以我发现但没有创建的混乱状态来适应此代码。

for key,row in df.iterrows():
    choices = str(list(df2.NAME_ID.unique()))
    names = process.extract(str(row['P1_ID']), choices, limit=2)[0][0]
    name = df2[df2['NAME_ID'] == names]['NAME']
    if not name.empty:
        df.loc[key,'Name'] = name





import pandas as pd

df = pd.read_clipboard(sep='\s\s+')  

GAME_DATE_EST   GAME_ID GAME_STATUS_TEXT    P1_ID   P2_ID   SEASON  P1_ID   PTS_P1
0   2020-01-01  21900504    Final   1610612764  1610612753  2019    1610612764  10
1   2020-01-01  21900505    Final   1610612752  1610612757  2019    1610612752  9
2   2020-01-01  21900506    Final   1610612749  1610612750  2019    1610612749  10
3   2020-01-01  21900507    Final   1610612747  1610612756  2019    1610612747  8
4   2019-12-31  21900497    Final   1610612766  1610612738  2019    1610612766  9

df2

     NAME_ID    STANDINGSDATE NAME      G   W   L   W_PCT
0   1610612747  2020-01-01    Math      34  27  7   0.79
1   1610612743  2020-01-01    John      33  23  10  0.70
2   1610612746  2020-01-01    Elias     35  24  11  0.69
3   1610612745  2020-01-01    Alexander 34  23  11  0.68
4   1610612742  2020-01-01    Michael   33  21  12  0.64 

希望您能理解并能帮助我

1 个答案:

答案 0 :(得分:1)

为此,您可以执行一个简单的join

.grid-container {
  align-items: start;
  display: grid;
  grid-gap: 10px;
}

.grid-column-count-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.grid-row-count-2 {
  grid-template-rows: repeat(2, min-content);
}

.col-1 {
  grid-column-start: 1;
}

.col-2 {
  grid-column-start: 2;
}

.row-1 {
  grid-row-start: 1;
}

.row-2 {
  grid-row-start: 2;
}

.rowspan-2 {
  grid-row-end: span 2;
}

.align-self-end {
  align-self: end;
}

.align-self-stretch {
  align-self: stretch;
}

根据您提供的数据,您可以尝试:

<div class="grid-container grid-column-count-2 grid-row-count-2">
  <div class="col-1 row-1 rowspan-2 align-self-stretch" style="background-color: #f2f2f2; border: 1px solid #bbb; overflow-y: scroll;"><span>div A</span><br>
    <span>div B</span><br><span>div C</span><br><span>div D</span><br></div>
  <input type="text" class="col-2 row-1" />
  <input type="text" class="col-2 row-2 align-self-end" />
  <input type="text" class="col-2 row-1" />
</div>