繁体   English   中英

如何根据条件更新单元格值?

[英]How to base on condition to update cell value?

大家,我是 Python 的绝对初学者,目前独自学习。 我遇到了一个问题,我希望我能找到有才华的人来教我如何解决这个问题。

  1. 我有两个不同的 excel 来比较...

     Data1.xlsx Data2.xlsx | Name | City | Reg Date | Gender | Check In | Check In Date | | Name | Reg Date | |Alex | Hong Kong | 2021-06-30 | Male | Y | 2021-06-30 | |Annie | 2021-07-01 | |Annie | Hong Kong | 2021-07-01 | Female | | | |Billy | 2021-07-02 | |Bob | Taipei | 2021-06-28 | Male | Y | 2021-06-28 | |Cathrine | 2021-07-03 | |Lucy | Tokyo | 2021-06-28 | Female | Y | 2021-06-28 | |David | 2021-07-04 | |David | London | 2021-07-04 | Male | | | |Eric | 2021-07-04 | |Kate | New York | 2021-07-03 | Female | | | |Cathrine | London | 2021-07-03 | Female | | | |Rose | Hong Kong | 2021-07-04 | Female | | |
  2. 使用NameReg Date作为键合并

    import openpyxl as xl import pandas as pd import numpy as np dt1 = pd.read_excel('Data1.xlsx') dt2 = pd.read_excel('Data2.xlsx') df_merge = pd.merge(dt1, dt2[['Name', 'Reg Date']], on=['Name', 'Reg Date'], how='left', indicator=True) Name City Reg Date Gender Check In Check In Date _merge 0 Alex Hong Kong 2021-06-30 Male Y 2021-06-30 left_only 1 Annie Hong Kong 2021-07-01 Female NaN NaN both 2 Bob Taipei 2021-06-28 Male Y 2021-06-28 left_only 3 Lucy Tokyo 2021-06-28 Female Y 2021-06-28 left_only 4 David London 2021-07-04 Male NaN NaN both 5 Kate New York 2021-07-03 Female NaN NaN left_only 6 Cathrine London 2021-07-03 Female NaN NaN both 7 Rose Hong Kong 2021-07-04 Female NaN NaN left_only
  3. 如何检查both是否相等以填写Check In is Y并将Reg Date复制到Check In Date

     for a in df_merge.iloc[:, [7]].values: if a == 'both':

使用 boolean 索引和fillna

cols = ['Check In', 'Check In Date']
mask = df_merged['_merge'].eq('both')

df_merged.loc[mask, cols] = \
    df_merged.loc[mask, cols].fillna({'Check In': 'Y',
                                      'Check In Date': df_merged['Reg Date']})

Output:

>>> df_merged
       Name       City    Reg Date  Gender Check In Check In Date     _merge
0      Alex  Hong Kong  2021-06-30    Male        Y    2021-06-30  left_only
1     Annie  Hong Kong  2021-07-01  Female        Y    2021-07-01       both
2       Bob     Taipei  2021-06-28    Male        Y    2021-06-28  left_only
3      Lucy      Tokyo  2021-06-28  Female        Y    2021-06-28  left_only
4     David     London  2021-07-04    Male        Y    2021-07-04       both
5      Kate   New York  2021-07-03  Female      NaN           NaN  left_only
6  Cathrine     London  2021-07-03  Female        Y    2021-07-03       both
7      Rose  Hong Kong  2021-07-04  Female      NaN           NaN  left_only

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM