简体   繁体   English

如何从另一个数据框更新一个数据框的特定列

[英]how to update specific column of one dataframe from another dataframe

the first dataframe is: 第一个数据帧是:

   data_date cookie_type   dau  next_dau  dau_7  dau_15
0   20181006    avg(0-d)  2288       NaN    NaN     NaN
1   20181006    avg(e-f)  2284       NaN    NaN     NaN
2   20181007    avg(e-f)  2296       NaN    NaN     NaN

the second dataframe is : 第二个数据帧是:

  data_date cookie_type  next_dau
0  20181006    avg(e-f)       908
1  20181006    avg(0-d)       904

how to update the first dataframe's next_dau from the second one i have tried combine_first and fillna, they seem not support multi-index: 如何从我尝试过的Combine_first和fillna的第二个数据帧更新第二个数据帧的next_dau,它们似乎不支持多索引:

cols = ['data_date', 'cookie_type']

    if (frame1 is not None and not frame1.empty):
        frame1.set_index(cols)
        print(frame1)
        print(next_day_dau)
        frame1.combine_first(next_day_dau.set_index(cols))
        frame1.combine_first(dau_7.set_index(cols))
        frame1.combine_first(dau_15.set_index(cols))

finally i solved this problem with help from "tianhua liao": 终于,我在“廖天华”的帮助下解决了这个问题:

            frame1.index = frame1.data_date.astype(str) + frame1.cookie_type
            next_day_dau.index = next_day_dau.data_date.astype(str) + next_day_dau.cookie_type
            dau_7.index = dau_7.data_date.astype(str) + dau_7.cookie_type
            dau_15.index = dau_15.data_date.astype(str) + dau_15.cookie_type
            # get_index
            next_day_dau_idx = frame1.index.isin(next_day_dau.index)
            dau_7_idx = frame1.index.isin(dau_7.index)
            dau_15_idx = frame1.index.isin(dau_15.index)
            #
            if any(next_day_dau_idx):
                frame1.loc[next_day_dau_idx, "next_dau"] = next_day_dau.next_dau
            if any(dau_7_idx):
                frame1.loc[dau_7_idx, "dau_7"] = dau_7.dau_7
            if any(dau_15_idx):
                frame1.loc[dau_15_idx, "dau_15"] = dau_15.dau_15

Multi-index is a complicated one. 多索引是一个复杂的索引。

Here is a simple way to solve it. 这是解决问题的简单方法。

frame1.index = frame1.data_date.astype(str) + frame1.cookie_type
frame2.index = frame2.data_date.astype(str) + frame2.cookie_type

frame1.loc[frame2.index,"next_dau"] = frame2.next_dau

After processing completed, you could remove the index. 处理完成后,您可以删除索引。

暂无
暂无

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

相关问题 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas 如何根据另一个 DataFrame 中的列更新 Pandas DataFrame 中的列 - How to update a column in pandas DataFrame based on column from another DataFrame 从另一列列表中的特定值填充一个数据框列 - Fill one Dataframe Column from specific value in list of another column 如何将特定值从一个 dataframe 填充到另一个 dataframe - How to fill specific values from one dataframe to another dataframe 如何使用熊猫中另一个数据框的值更新一个数据框 - How to update one dataframe using values from another dataframe in pandas 如何将数据从一个 dataframe 添加到另一个 dataframe 上的新列 - how to add data from one dataframe to a new column on another dataframe 如何将一列从一个数据帧添加到另一个数据帧? - How can I add a column from one dataframe to another dataframe? 如何迭代每一行并从一个 dataframe 的特定列中找到下一个匹配列值并将其与另一个 dataframe 进行比较? - How to iterate each row and find the next matching column value from a specific column from one dataframe and comparing it to another dataframe? 如何在匹配来自不同数据帧的另一列的行后更新一列 dF 的值 - How to update the value of one column dF after matching the rows of from another column from different dataframe 如何将 append 一个 dataframe 变成另一个 dataframe 作为一列 - How to append one dataframe into another dataframe as a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM