2016-06-16 61 views
1

我已經使用pandas包編寫了以下python代碼。創建熊貓數據框時出現TypeError

import matplotlib.pyplot as plt 
import pandas as pd 
import numpy as np 
from pandas import Series 

csv = pd.read_csv('train.csv') 
df_csv = pd.DataFrame(csv) 

PassengerId = np.array(df_csv['PassengerId']) 
Age = np.array(df_csv['Age']) 
Pclass = np.array(df_csv['Pclass']) 
Sex = np.array(df_csv['Sex']) 

i = 0 
while i < 891: 
    if Sex[i] == 'male': 
     Sex[i] = 0 
     i = i + 1 
    else: 
     Sex[i] = 1 
     i = i + 1 
Sex = np.array(Sex) 
new_df = pd.DataFrame[ 
    'PassengerId': Series(PassengerId), 
    'Age': Series(Age), 
    'Pclass': Series(Pclass), 
    'Sex': Series(Sex) 
] 

print(new_df) 

我試圖通過讀取csv文件,存儲幾列作爲numpy的陣列然後更換一個陣列的值來創建一個數據幀。當我再次合併這些數組作爲數據幀,我得到以下錯誤

D:\Projects\Titanic>python python.py 
Traceback (most recent call last): 
    File "python.py", line 27, in <module> 
    'Sex': Sex 
TypeError: 'type' object is not subscriptable 

請幫我。在此先感謝

+0

更換

new_df = pd.DataFrame[ 'PassengerId': Series(PassengerId), 'Age': Series(Age), 'Pclass': Series(Pclass), 'Sex': Series(Sex) ] 

這是無效的:'new_df = pd.DataFrame [ 'PassengerId':系列(PassengerId), '時代':系列(Age), 'Pclass':Series(Pclass), 'Sex':Series(Sex) ]'it should be round parenthese'()'另外你應該傳遞一個字典'new_df = pd.DataFrame({ 'PassengerId' :系列(PassengerId), '年齡':系列(年齡), 'Pclass':系列(Pclass), '性別':系列(性別) })' – EdChum

+0

謝謝!它工作完美! –

回答

0

嘗試用

new_df = pd.DataFrame({ 
    'PassengerId': Series(PassengerId), 
    'Age': Series(Age), 
    'Pclass': Series(Pclass), 
    'Sex': Series(Sex) 
}) 
+0

比你!它非常完美! –