python - 按列名加入 Pandas 数据框

标签 python pandas dataframe

我有两个具有以下列名称的 DataFrame:

frame_1:
event_id, date, time, county_ID

frame_2:
countyid, state

我想通过加入(左)county_ID = countryid 来获得一个包含以下列的 DataFrame:

joined_dataframe
event_id, date, time, county, state

如果我想加入的列不是索引,我不知道该怎么做。

最佳答案

您可以使用 pd.mergeleft_onright_on 选项如下:

pd.merge(frame_1, frame_2, left_on='county_ID', right_on='countyid')

或等价于 DataFrame.merge :

frame_1.merge(frame_2, left_on='county_ID', right_on='countyid')

如果您只想在键位于左侧 DataFrame 中时合并,我不确定问题。如果是这种情况,那么以下将执行此操作(以上将实际上进行多对多合并)

pd.merge(frame_1, frame_2, how='left', left_on='county_ID', right_on='countyid')

或者

frame_1.merge(frame_2, how='left', left_on='county_ID', right_on='countyid')

关于python - 按列名加入 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20375561/

相关文章:

python - 读取 CSV 列,如果满足要求,写入单元格

python-3.x - python中的单元测试 - 在测试中创建数据帧

python - 从右到左对齐数据

python - 用另一个值替换 Pandas 数据框列中的几个值

python - 无法在 VSCode 中打开 jupyter notebook

python - 如何使用 KerasClassifier 使用 LSTM 进行序列分类

python - 促进代码生成的最佳 Python 模板库

python - Pandas :带有 multiIndex 数据框的条形图

python - 添加新行以计算现有 pandas 数据帧的总和和平均值

Python3 argparse : When passing string as arg run funcA, 如果虚线 arg 运行另一个函数