2017-05-28 179 views
0

我一直在探索titanic dataset。我试图创建一个 dataframe这将有两个单独列中的泰坦尼克号沉没的人以及那些没有的人的年龄。python,pandas:创建数据帧时InvalidIndexError

train = pd.read_csv('train.csv') 
    test = pd.read_csv('test.csv')  
    whole = pd.concat([train, test]) 
    df = pd.DataFrame({'survived': whole['Age'][whole['Survived'] == 1], 
         'died': whole['Age'][whole['Survived'] == 0]}) 

但我收到此错误

pandas.indexes.base.InvalidIndexError: Reindexing only valid with uniquely valued Index objects

我在做什么错?

+0

它运行这种变化不会对大熊猫0.20.1错误。 – ayhan

+0

改变这个:'whole = pd.concat([train,test])'''whole = pd.concat([train,test])。reset_index(drop = True)' – Nain

+0

@Nain是的,它工作。你能解释什么是问题吗? – Sounak

回答

2

让你的代码 whole = pd.concat([train, test]).reset_index(drop=True)

+2

我们可以使用:'pd.concat([train,test],ignore_index = True)') – MaxU

+0

@MaxU This works too。将'ignore_index'设置为'True'会发生什么? – Sounak

+0

'pd.concat'会为您创建一个新的默认索引('np.arange(len(concatenated_df))'),所以它不需要连接两个现有索引,然后再次删除它并创建一个新索引。 。 – MaxU