【发布时间】:2021-12-07 05:15:37
【问题描述】:
我有两个独立的数据框,它们共享一个项目编号。在type_df 中,项目号是索引。在time_df 中,项目号是一列。我想计算type_df 中具有Project Type 的2 的行数。我正在尝试使用pandas.merge() 来做到这一点。它在使用两列时效果很好,但不是索引。我不确定如何引用索引以及merge 是否是正确的方法。
import pandas as pd
type_df = pd.DataFrame(data = [['Type 1'], ['Type 2']],
columns=['Project Type'],
index=['Project2', 'Project1'])
time_df = pd.DataFrame(data = [['Project1', 13], ['Project1', 12],
['Project2', 41]],
columns=['Project', 'Time'])
merged = pd.merge(time_df,type_df, on=[index,'Project'])
print merged[merged['Project Type'] == 'Type 2']['Project Type'].count()
错误:
名称“索引”未定义。
期望的输出:
2
【问题讨论】:
标签: python python-2.7 pandas merge