【问题标题】:Joining dataframes in Pandas在 Pandas 中加入数据框
【发布时间】:2017-04-22 11:41:23
【问题描述】:

我收到了错误:

TypeError: 'method' 对象不可下标

当我尝试加入两个 Pandas 数据框时...

看不出他们有什么问题!

有关信息,请做 Kaggle 泰坦尼克号问题:

titanic_df.head()
Out[102]:
Survived    Pclass  SibSp   Parch   Fare    has_cabin   C   Q   title   Person
0   0   3   1   0   7.2500  1   0.0 0.0 Mr  male
1   1   1   1   0   71.2833 0   1.0 0.0 Mrs female
2   1   3   0   0   7.9250  1   0.0 0.0 Miss    female
3   1   1   1   0   53.1000 0   0.0 0.0 Mrs female
4   0   3   0   0   8.0500  1   0.0 0.0 Mr  male
In [103]:

sns.barplot(x=titanic_df["Survived"],y=titanic_df["title"])
Out[103]:
<matplotlib.axes._subplots.AxesSubplot at 0x11b5edb00>

In [125]:

title_dummies=pd.get_dummies(titanic_df["title"])
title_dummies=title_dummies.drop([" Don"," Rev"," Dr"," Col"," Capt"," Jonkheer"," Major"," Mr"],axis=1)
title_dummies.head()
Out[125]:
Lady    Master  Miss    Mlle    Mme Mrs Ms  Sir the Countess
0   0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1   0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0
2   0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
3   0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0
4   0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
In [126]:

titanic_df=title_dummies.join[titanic_df]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-126-a0e0fe306754> in <module>()
----> 1 titanic_df=title_dummies.join[titanic_df]

TypeError: 'method' object is not subscriptable

【问题讨论】:

标签: python pandas join dataframe


【解决方案1】:

您需要在DataFrame.join 函数中将[] 更改为()

titanic_df=title_dummies.join(titanic_df)
print (titanic_df)
   Lady  Master  Miss  Mlle  Mme  Mrs   Ms  Sir  the  Countess  Survived  \
0     0     0.0   0.0   0.0  0.0  0.0  0.0  0.0  0.0       0.0         0   
1     1     0.0   0.0   0.0  0.0  0.0  1.0  0.0  0.0       0.0         1   
2     2     0.0   0.0   1.0  0.0  0.0  0.0  0.0  0.0       0.0         1   
3     3     0.0   0.0   0.0  0.0  0.0  1.0  0.0  0.0       0.0         1   
4     4     0.0   0.0   0.0  0.0  0.0  0.0  0.0  0.0       0.0         0   

   Pclass  SibSp  Parch     Fare  has_cabin    C    Q title  Person  
0       3      1      0   7.2500          1  0.0  0.0    Mr    male  
1       1      1      0  71.2833          0  1.0  0.0   Mrs  female  
2       3      0      0   7.9250          1  0.0  0.0  Miss  female  
3       1      1      0  53.1000          0  0.0  0.0   Mrs  female  
4       3      0      0   8.0500          1  0.0  0.0    Mr    male  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2018-08-04
    • 2014-07-10
    • 2016-11-06
    • 2015-04-22
    相关资源
    最近更新 更多