简体   繁体   English

如何基于另一列将 append 数据转换为 dataframe?

[英]How to append data to dataframe based on another column?

I would like to append values from one dataframe into another by checking if they contain the same Document Number.我想通过检查它们是否包含相同的文档编号,将 append 值从一个 dataframe 转换为另一个。

ip_df: ip_df:

        CardName     DocNum    DocDate      DocTotal    DocNum2    PaidToDate   Balance
0       CompanyA  800100001 2021-03-01  10000.000000  920000000  10000.000000  0.000000
1       CompanyA  800100002 2021-03-01  20000.000000  920000000  20000.000000  0.000000
2       CompanyA  800100003 2021-03-01  30000.000000  920000000  30000.000000  0.000000
3       CompanyA  800100004 2021-03-01  40000.000000  920000000  40000.000000  0.000000
4       CompanyA  800100005 2021-03-01  50000.000000  920000000  50000.000000  0.000000
..      ...        ...        ...           ...        ...           ...       ...
94      CompanyY  800100006 2021-03-01  60000.000000  920000005  60000.000000  0.000000
95      CompanyY  800100007 2021-03-01  70000.000000  920000005  70000.000000  0.000000
96      CompanyY  800100008 2021-03-01  80000.000000  920000005  80000.000000  0.000000
97      CompanyZ  800100009 2021-03-01  90000.000000  920000006  90000.000000  0.000000
98      CompanyZ  800100010 2021-03-01  11000.000000  920000006  11000.000000  0.000000

[99 rows x 7 columns]

ar_df: ar_df:

        BPCode       Balance Currency    DueDate  BPName  TransId       Ref1      Payment Received  Bank Charge
0     XXXXXXXX  10000.000000      USD 2020-09-29  CompanyA   503378  800100001          0            0
1     XXXXXXXX  20000.000000      USD 2021-03-01  CompanyA   543103  800100002          0            0
2     XXXXXXXX  30000.000000      USD 2021-03-01  CompanyA   543171  800100003          0            0
3     XXXXXXXX  40000.000000      USD 2021-03-01  CompanyA   544205  800100004          0            0
4     XXXXXXXX  50000.000000      USD 2021-03-01  CompanyA   544222  800100005          0            0
...        ...           ...      ...        ...  ...      ...        ...               ...          ...
3763  XXXXXXXX  60000.000000      USD 2021-03-02  CompanyY   548612  800100006          0            0
3764  XXXXXXXX  70000.000000      USD 2021-03-02  CompanyY   547727  800100007          0            0
3765  XXXXXXXX  80000.000000      USD 2021-03-30  CompanyY   553819  800100008          0            0
3766  XXXXXXXX  90000.000000      USD 2021-04-01  CompanyZ   547707  800100009          0            0
3767  XXXXXXXX  11000.000000      USD 2021-04-29  CompanyZ   556102  800100010          0            0

[3768 rows x 9 columns]

I am trying to do this:我正在尝试这样做:

for row in ip_df:
if ip_df.row['DocNum'] == ar_df.row['Ref1']:
    ap_df.row['Payment Received'] = ip_df.row['PaidToDate']

But I am not too sure of the correct way to doing this.但我不太确定这样做的正确方法。

I have tried searching around, but mostly, the answers always seem to point towards checking against scalar values only.我尝试过四处搜索,但大多数情况下,答案似乎总是指向仅检查标量值。

What I want to achieve is checking if in a row in ip_df, whether 'DocNum' can be found in the ap_df's 'Ref1' column, and if true, set ap_df's 'Payment Received' row to be a value from ip_df's 'PaidToDate' row.我想要实现的是检查是否在 ip_df 的一行中,是否可以在 ap_df 的“Ref1”列中找到“DocNum”,如果为真,则将 ap_df 的“已收到付款”行设置为 ip_df 的“PaidToDate”行中的值.

The end result should look like:最终结果应如下所示:

        BPCode       Balance Currency    DueDate  BPName  TransId       Ref1      Payment Received  Bank Charge
0     XXXXXXXX  10000.000000      USD 2020-09-29  CompanyA   503378  800100001          10000.000000 0
1     XXXXXXXX  20000.000000      USD 2021-03-01  CompanyA   543103  800100002          20000.000000 0
2     XXXXXXXX  30000.000000      USD 2021-03-01  CompanyA   543171  800100003          30000.000000 0
3     XXXXXXXX  40000.000000      USD 2021-03-01  CompanyA   544205  800100004          40000.000000 0
4     XXXXXXXX  50000.000000      USD 2021-03-01  CompanyA   544222  800100005          50000.000000 0
...        ...           ...      ...        ...  ...      ...        ...               ...          ...
3763  XXXXXXXX  60000.000000      USD 2021-03-02  CompanyY   548612  800114258          60000.000000 0
3764  XXXXXXXX  70000.000000      USD 2021-03-02  CompanyY   547727  800113975          70000.000000 0
3765  XXXXXXXX  80000.000000      USD 2021-03-30  CompanyY   553819  800115292          80000.000000 0
3766  XXXXXXXX  90000.000000      USD 2021-04-01  CompanyZ   547707  800113957          90000.000000 0
3767  XXXXXXXX  11000.000000      USD 2021-04-29  CompanyZ   556102  800115741          11000.000000 0

[3768 rows x 9 columns]

Thank you for any help in advance!!感谢您提前提供任何帮助!

Use df.merge on a subset of columns with df.rename :在具有df.rename df.merge

In [220]: ar_df['Ref1'] = ar_df['Ref1'].str.replace('', np.nan).astype(int)
In [221]: ip_df['DocNum'] = ar_df['DocNum'].str.replace('', np.nan).astype(int)

In [222]: ar_df.merge(ip_df[['DocNum', 'PaidToDate']], left_on='Ref1', right_on='DocNum').drop(['Payment Received', 'DocNum'], 1).rename(columns={'PaidToDate':'Payment Received'})
Out[222]: 
     BPCode  Balance Currency     DueDate    BPName  TransId       Ref1  Bank_Charge  Payment_Received
0  XXXXXXXX  10000.0      USD  2020-09-29  CompanyA   503378  800100001            0           10000.0
1  XXXXXXXX  20000.0      USD  2021-03-01  CompanyA   543103  800100002            0           20000.0
2  XXXXXXXX  30000.0      USD  2021-03-01  CompanyA   543171  800100003            0           30000.0
3  XXXXXXXX  40000.0      USD  2021-03-01  CompanyA   544205  800100004            0           40000.0
4  XXXXXXXX  50000.0      USD  2021-03-01  CompanyA   544222  800100005            0           50000.0

暂无
暂无

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

相关问题 如何将 append 一个 dataframe 变成另一个 dataframe 作为一列 - How to append one dataframe into another dataframe as a column 对基于另一列的列求和并将该列追加到数据框中 - Sum a column based on another column and append that column in dataframe pandas数据框根据另一数据框中的值将值追加到一列 - pandas dataframe append values to one column based on the values in another dataframe 我如何根据列单元格值和 append 查找一个 dataframe 上的一行到另一个 dataframe 上的一行? - How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe? append 列的值基于 dataframe 中另一列的值 - append column with values based on values of another column in the dataframe 如何使用基于条件的值将 append 列到 dataframe - How to append a column to a dataframe with values based on condition 如何将一个数据帧的列值附加到另一个数据帧的列 - How to append column values of one dataframe to column of another dataframe 仅将具有列名的数据框追加到具有数据的另一个数据框 - Append dataframe with column names alone to another dataframe with data 如何根据行/列名称将一个数据框的列附加为另一个数据框的行? - How to append column of one data frame as row of another data frame based on row/column name? 如何根据另一列中的数据填充 dataframe 中的列并在 python 中的另一列上进行条件/切换 - How to populate a column in a dataframe based on data in another column and condition /switch on another column in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM