python - 在 Pandas 中加入数据框

标签 python pandas join dataframe

我收到错误:

类型错误:“方法”对象不可下标

当我尝试加入两个 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

最佳答案

您需要将 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  

关于python - 在 Pandas 中加入数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43558769/

相关文章:

python - 如何根据 Pandas 中另一列的条件比较同一列中的日期?

多个 INNER JOIN 上的 SQL SUM

php - Symfony/Doctrine ManyToMany 按照分配的顺序

python - 如何处理 PythonCard 中的 mouseMiddleDrag 事件?

python - 永久改变 sys.path ...好主意还是不好?

python - Python map(None, fcn()) 是否有更简单的构造?

python - 将带有逗号空格逗号的 CSV 转换为浮点型

python pandas dataframe 从其他列的单元格创建新列

hibernate - HQL:如果有时为空,如何筛选属性

python - 生成字符串列表的所有排列