熊猫:使用其他列名附加数据框

时间:2019-10-18 10:44:48

标签: python pandas

我已经通过API提取了数据,并且源系统创建了一个新字段。我正在尝试合并以下2个项目。除此以外,还有更多的领域,但这很重要。如何合并以下数据集?

字段名称将始终相同,但将来可能会删除其他列。

Audit_ID    Start Time           End Time   
1           02/09/2019 05:00     02/09/2019 10:45   

新数据

Audit_ID    Start Time           End Time            Shift
2           03/09/2019 03:00     03/09/2019 10:45    Afters

这就是我想要的样子:

Audit_ID    Start Time           End Time            Shift
1           02/09/2019 05:00     02/09/2019 10:45    
2           03/09/2019 03:00     03/09/2019 10:45    Afters

当我运行代码时:

joined_rows = pd.concat(data1,data2],轴= 0

这会导致错误:

joined_rows = pd.concat([data1, data2])

    AssertionError                            Traceback (most recent call last)
<ipython-input-46-469b3f9d61b5> in <module>()
----> 1 joined_rows = pd.concat(data1 , data2], axis=0)
      2 joined_rows

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, sort, copy)
    256     )
    257 
--> 258     return op.get_result()
    259 
    260 

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\reshape\concat.py in get_result(self)
    471 
    472             new_data = concatenate_block_managers(
--> 473                 mgrs_indexers, self.new_axes, concat_axis=self.axis, copy=self.copy
    474             )
    475             if not self.copy:

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals\managers.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
   2057         blocks.append(b)
   2058 
-> 2059     return BlockManager(blocks, axes)

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals\managers.py in __init__(self, blocks, axes, do_integrity_check)
    141 
    142         if do_integrity_check:
--> 143             self._verify_integrity()
    144 
    145         self._consolidate_check()

C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals\managers.py in _verify_integrity(self)
    348                 "Number of manager items must equal union of "
    349                 "block items\n# manager items: {0}, # "
--> 350                 "tot_items: {1}".format(len(self.items), tot_items)
    351             )
    352 

AssertionError: Number of manager items must equal union of block items
# manager items: 44, # tot_items: 48

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

示例代码:

dict1 = {'Audit_ID':['1'],'Start_time':['02/09/2019 05:00'],'End_Time':['02/09/2019 10:45']}
dict2 = {'Audit_ID':['2'],'Start_time':['03/09/2019 05:00'],'End_Time':['03/09/2019 10:45'], 'shift':['Afters']}
df1 = pd.DataFrame.from_dict(dict1)
df2 = pd.DataFrame.from_dict(dict2)
# above code is to create your dataset

res = pd.concat([df1, df2], axis=0)
print(res)

结果:

  Audit_ID          End_Time        Start_time   shift
0        1  02/09/2019 10:45  02/09/2019 05:00     NaN
0        2  03/09/2019 10:45  03/09/2019 05:00  Afters