我不能和熊猫一起做桌子

时间:2018-12-17 02:47:28

标签: python pandas numpy

由于某种原因,我的代码有问题,它不起作用。但是这两个函数的作用几乎相同,但是在第二个函数中,代码不起作用,我也不知道为什么。

下面是代码:

    import numpy
    import pandas
    import statsmodels.api as sm
    def simple_heuristic(file_path):
        id_pass = []
        name = []
        suviver = []
        df = pandas.read_csv(file_path)
        for passenger_index, passenger in df.iterrows():
            passenger_id = passenger['PassengerId']

            if passenger['Sex'] == 'male':
                id_pass.append(passenger_id)
                name.append(passenger['Name'])
                suviver.append(1)
            else:
                id_pass.append(passenger_id)
                name.append(passenger['Name'])
                suviver.append(0)

        predictions = {'id':pandas.Series(id_pass),
                        'Name': name,
                        'Suviver': pandas.Series(suviver)}

        return pandas.DataFrame(predictions)
    def complex_heuristic(file_path):
        id_pass = []
        name = []
        sex = []
        suviver =[]
        socileco = []
        df = pandas.read_csv(file_path)
        for passenger_index, passenger in df.iterrows():
            passenger_id = passenger['PassengerId']

            if passenger['Sex'] == 'female' or (passenger['Age'] < 18 and passenger['Pclass']==1) :
                id_pass.append(passenger_id)
                name.append(passenger['Name'])
                suviver.append(1)
            else:
                id_pass.append( passenger_id)
                #name.append(passenger['Name'])
                #sex.append(passenger['Sex'])
                suviver.append(0)
                #socileco.append(passenger['Pclass'])

        predictions = {'Idx' : pandas.Series(id_pass),
                        'Name': name,
                        'Sobreviveu': pandas.Series(suviver)},
        return pandas.DataFrame(predictions)
    print simple_heuristic('titanic-data.csv')
    print complex_heuristic('titanic-data.csv')

但是总是我尝试运行代码时,我仍然遇到相同的错误

File "heuristic.py", line 161, in <module>
    print complex_heuristic('titanic-data.csv')
  File "heuristic.py", line 159, in complex_heuristic
    return pandas.DataFrame(predictions)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 422, in __init__
    raise ValueError('DataFrame constructor not properly called!')
ValueError: DataFrame constructor not properly called!

我做了同样的事情,但是其中一个代码起作用了,但是第二个却没有

0 个答案:

没有答案