Pandas:合并两个表时出错,错误 set_index
Pandas: Error when merging two tables, Error with set_index
在此先感谢您的帮助,这是我的问题:
我已成功将我的 df 加载到 ipython 笔记本中,然后我 运行 在上面分组:
station_count = station.groupby('landmark').count()
产生了这样的 table:
现在我正在尝试将它与另一个合并 table:
dock_count_by_station = station.groupby('landmark').sum()
这也是一个简单的 group by on the same table,但是合并产生了一个错误:
类型错误:无法连接非 NDFrame 对象
使用此代码:
dock_count_by_station.merge(station_count)
我认为问题是我需要在合并它们之前设置两个 table 的索引,但我不断收到以下代码的错误:
pandas/index.pyx 在 pandas.index.IndexEngine.get_loc (pandas/index.c:3979)()
pandas/index.pyx 在 pandas.index.IndexEngine.get_loc (pandas/index.c:3843)()
pandas/hashtable.pyx 在 pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12265)()
pandas/hashtable.pyx 在 pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12216)()
按键错误:'landmark'
station_count.set_index('landmark')
使用join
您可以使用 join
,它会根据索引合并表格。您可能还希望指定联接类型(例如 'outer'、'inner'、'left' 或 'right')。您有重叠的列名称(例如 station_id),因此您需要指定一个后缀。
>>> dock_count_by_station.join(station_count, rsuffix='_rhs')
dockcount lat long station_id dockcount_rhs installation lat_rhs long_rhs name station_id_rhs
landmark
Mountain View 117 261.767433 -854.623012 210 7 7 7 7 7 7
Palo Alto 75 187.191873 -610.767939 180 5 5 5 5 5 5
Redwood City 115 262.406232 -855.602755 224 7 7 7 7 7 7
San Francisco 665 1322.569239 -4284.054814 2126 35 35 35 35 35 35
San Jose 249 560.039892 -1828.370075 200 15 15 15 15 15 15
使用merge
请注意,您的 landmark
索引是在您执行 groupby
时默认设置的。如果您不希望发生这种情况,您始终可以使用 as_index=False
,但是您必须使用 merge
而不是 join
。
dock_count_by_station = station.groupby('landmark', as_index=False).sum()
station_count = station.groupby('landmark', as_index=False).count()
>>> dock_count_by_station.merge(station_count, on='landmark', suffixes=['_lhs', '_rhs'])
landmark dockcount_lhs lat_lhs long_lhs station_id_lhs dockcount_rhs installation lat_rhs long_rhs name station_id_rhs
0 Mountain View 117 261.767433 -854.623012 210 7 7 7 7 7 7
1 Palo Alto 75 187.191873 -610.767939 180 5 5 5 5 5 5
2 Redwood City 115 262.406232 -855.602755 224 7 7 7 7 7 7
3 San Francisco 665 1322.569239 -4284.054814 2126 35 35 35 35 35 35
4 San Jose 249 560.039892 -1828.370075 200 15 15 15 15 15 15
在此先感谢您的帮助,这是我的问题:
我已成功将我的 df 加载到 ipython 笔记本中,然后我 运行 在上面分组:
station_count = station.groupby('landmark').count()
产生了这样的 table:
现在我正在尝试将它与另一个合并 table:
dock_count_by_station = station.groupby('landmark').sum()
这也是一个简单的 group by on the same table,但是合并产生了一个错误:
类型错误:无法连接非 NDFrame 对象
使用此代码:
dock_count_by_station.merge(station_count)
我认为问题是我需要在合并它们之前设置两个 table 的索引,但我不断收到以下代码的错误:
pandas/index.pyx 在 pandas.index.IndexEngine.get_loc (pandas/index.c:3979)()
pandas/index.pyx 在 pandas.index.IndexEngine.get_loc (pandas/index.c:3843)()
pandas/hashtable.pyx 在 pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12265)()
pandas/hashtable.pyx 在 pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12216)()
按键错误:'landmark'
station_count.set_index('landmark')
使用join
您可以使用 join
,它会根据索引合并表格。您可能还希望指定联接类型(例如 'outer'、'inner'、'left' 或 'right')。您有重叠的列名称(例如 station_id),因此您需要指定一个后缀。
>>> dock_count_by_station.join(station_count, rsuffix='_rhs')
dockcount lat long station_id dockcount_rhs installation lat_rhs long_rhs name station_id_rhs
landmark
Mountain View 117 261.767433 -854.623012 210 7 7 7 7 7 7
Palo Alto 75 187.191873 -610.767939 180 5 5 5 5 5 5
Redwood City 115 262.406232 -855.602755 224 7 7 7 7 7 7
San Francisco 665 1322.569239 -4284.054814 2126 35 35 35 35 35 35
San Jose 249 560.039892 -1828.370075 200 15 15 15 15 15 15
使用merge
请注意,您的 landmark
索引是在您执行 groupby
时默认设置的。如果您不希望发生这种情况,您始终可以使用 as_index=False
,但是您必须使用 merge
而不是 join
。
dock_count_by_station = station.groupby('landmark', as_index=False).sum()
station_count = station.groupby('landmark', as_index=False).count()
>>> dock_count_by_station.merge(station_count, on='landmark', suffixes=['_lhs', '_rhs'])
landmark dockcount_lhs lat_lhs long_lhs station_id_lhs dockcount_rhs installation lat_rhs long_rhs name station_id_rhs
0 Mountain View 117 261.767433 -854.623012 210 7 7 7 7 7 7
1 Palo Alto 75 187.191873 -610.767939 180 5 5 5 5 5 5
2 Redwood City 115 262.406232 -855.602755 224 7 7 7 7 7 7
3 San Francisco 665 1322.569239 -4284.054814 2126 35 35 35 35 35 35
4 San Jose 249 560.039892 -1828.370075 200 15 15 15 15 15 15