让我们从头开始编写和理解Python中的决策树!第3部分。熊猫数据分析库

哈Ha!敬请关注“ Python the 0かンンンツリーをを理解理解理解理解理解理解理解3 3デ(3.ーラライブラリリリandaリanda熊猫编)



这是系列文章中的第三篇。链接到先前的文章:第一第二篇



在本文中,我将解释如何与Pandas库一起创建决策树。



3.1导入库



#  pandas  ,        pd
import pandas as pd


3.2数据框和系列



熊猫使用诸如数据框架和系列之类的结构。

让我们看一下下面的类似Excel的表。



一行数据称为“系列”,各列称为该数据的属性,整个表称为“数据框架”。





3.3创建数据框



我们使用read_excelExcelWriter连接Excel电子表格

#  Excel   ,     ipynb
df0 = pd.read_excel("data_golf.xlsx")
 
#  DataFrame  HTML  
from IPython.display import HTML
html = "<div style='font-family:\"メイリオ\";'>"+df0.to_html()+"</div>"
HTML(html)
 
#   Excel  (with   f.close)
with pd.ExcelWriter("data_golf2.xlsx") as f: 
       df0.to_excel(f)


从字典(关联数组)创建数据框:字典将DataFrame列的数据汇总在一起



#   :    
 
d = {
    "":["","","","","","","","","","","","","",""],
    "":["","","","","","","","","","","","","",""], 
    "":["","","","","","","","","","","","","",""],
    "":["","","","","","","","","","","","","",""],
 
"":["×","×","○","○","○","×","○","×","○","○","○","○","○","×"],
}
df0 = pd.DataFrame(d)


从数组创建数据框:从数据框收集数据



#   :     
d = [["","","","","×"],
     ["","","","","×"],
     ["","","","","○"],
     ["","","","","○"],
     ["","","","","○"],
     ["","","","","×"],
     ["","","","","○"],
     ["","","","","×"],
     ["","","","","○"],
     ["","","","","○"],
     ["","","","","○"],
     ["","","","","○"],
     ["","","","","○"],
     ["","","","","×"],
    ]
#        columns  index .  ,   ,    .

df0 = pd.DataFrame(d,columns=["","","","",""],index=range(len(d)))


3.4从表中获取信息



#    
 
#    
print(df0.shape) #  (14, 5)
 
#    
print(df0.shape[0]) #  14
 
#   
print(df0.columns) #  Index(['', '', '', '', ''], dtype='object')
 
#    (  df0 -    )
print(df0.index) #  RangeIndex(start=0, stop=14, step=1)


3.5检索LOC ILOC



#  
 
#  ,    
#       №1 ( )
print(df0.loc[1,""]) #  

#  ,       
#        1,2,4,      Data Frame-  
df = df0.loc[[1,2,4],["",""]]
print(df)
# 
#                    
# 1                               ×
# 2                        ○
# 3                            ○
# 4                            ○

# iloc     .    0.
#      1  3,    .   iloc  ,   1:4,  4-   . 
df = df0.iloc[1:4,:-1]
print(df)
# 
#                
# 1                              
# 2                    
# 3                        


#      (Series)
#      . s  Series
s = df0.iloc[0,:]
#  ,    ,      s[" "]
print(s[""]) #  

#       (numpy.ndarray).
print(df0.values)


3.6遍历数据,以迭代方式 遍历数据



#  ,  
#     .     .
for i,row in df0.iterrows():
    # i    ( ), row  Series
    print(i,row)
    pass

#     .    .
for i,col in df0.iteritems():
    # i   , col  Series
    print(i,col)
    pass


3.7 value_counts的频率



#   
#      . s  Series
s = df0.loc[:,""]

#     
print(s.value_counts())
# 
#     5
#     5
#     4
# Name: , dtype: int64

# ,   ,   “”
print(s.value_counts()[""]) #  5


3.8检索特定查询数据



#   
#  ,   - .
print(df0.query("==''"))
# 
#                    
# 0                            ×
# 1                            ×
# 7                            ×
# 8                            ○
# 10                                ○

#  ,   - ,     
print(df0.query("=='' and =='○'"))
# 
#                    
# 8                            ○
# 10                                ○

#  ,   - ,     
print(df0.query("=='' or =='○'"))
# 
#                    
# 0                            ×
# 1                            ×
# 2                        ○
# 3                            ○
# 4                            ○
# 6                        ○
# 7                            ×
# 8                            ○
# 9                                ○
# 10                                ○
# 11                            ○
# 12                                ○


谢谢阅读!



如果您告诉我们您是否喜欢本文,我们将非常高兴,翻译是否清晰,对您有用吗?



All Articles