python - 根据索引是否存在于两个数据框中,选择并添加另一个数据框中的列值

标签 python pandas dataframe

我有两个数据框,我们称它们为 AB,具有相同的索引(人员 ID),但某些 ID 可能位于 A 而不是 B,并且反之亦然。此外,ID 在 B 中是非唯一的,而在数据帧 A 中是唯一的,所以我想

我想检查B以查看是否存在某些ID,然后针对该特定ID将最大B标签的一列添加到A中。

我尝试编写下面的函数作为 pandas .apply() 函数的参数。

def add_labels_to_dataframe(train_df,
                        id_col_name='person_id',
                        label_name="max_progress",
                        label_filepath=LABELS_SRC_FILE,
                        default_value=-1,
                        save=True):
"""
Add labels column to train_df

:param train_df: (DataFrame)
    the training dataframe that needs labels
:param id_col_name: (str)
    name of the ID column to use
:param label_name: (str)
    the column name of the label to use (score/progress/is_X/etc)
:param label_filepath: (str)
    filepath with IDs and associated labels
:param default_value: (int, or anything)
    The default label to give when a person_id has no associated label
:return: (DataFrame)
    updated dataframe with labels
"""
labels_df = pd.read_csv(label_filepath)

def get_max_score(row):
    """
    DataFrame function to select max score when multiple exist per ID

    :param row: (DataFrame)
        A single row of the dataframe being modified
    :return: (int)
        returns elements of a Series that becomes a new column of the DataFrame
    """
    # if person_id is in labels, then get max of labels
    pdb.set_trace()
    pid_labels_df = labels_df[row[id_col_name].isin(labels_df[id_col_name])]

    if not pid_labels_df.empty and not pd.isnull(pid_labels_df[label_name].max()):
        return 1 + pid_labels_df[label_name].max()

    return default_value


train_df[label_name] = train_df.apply(get_max_score, axis=1)

if save:
    train_df.to_csv(LABELED_TRAIN_DF_PATH)

return train_df

ValueError: ('Can only compare identically-labeled Series objects', 'occurred at index 0')

我知道我可以将两个数据帧索引转换为Python列表,检查值是否存在,然后创建一个新的数据帧,将旧行映射到标记值或某些默认-1,但我试图在Pandas中完成这一切,以便利用矢量化。

有人可以帮助我找到一种仅使用数据帧操作而不是转换为 Python 列表的简洁方法吗?

最佳答案

我认为*您将能够使用 groupby transform 来做到这一点:

df[label_name] = df.groupby("person_id").transform("max")

* 准确阅读代码试图执行的操作有点困难...

关于python - 根据索引是否存在于两个数据框中,选择并添加另一个数据框中的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56368717/

相关文章:

python - 尝试启动应用程序时无法导入 Django 错误

Python - 如何按频率检查数字组合

python - 时间序列数据集将数据拆分为大小相等的 block

python - 将 pandas'to_html' 保存为文件

r - 在设置的列数上合并 data.frame 列,删除 na,除非行中没有足够的值

python - scons/SConscript 文件的缩进错误

python - zip 函数帮助处理元组

python - 如何构建 celery 任务

python - 将 result_type 与 pandas apply 函数一起使用

python - 如何从满足条件的数据框中提取列和行索引