程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python如何訪問csv文件中一列的元素個數

編輯:Python

import pandas as pdimport matplotlib.pyplot as pltimport osplt.rcParams['font.sans-serif']=['SimHei']plt.rcParams['axes.unicode_minus']=Falsedef dataDescribeVisualization(): while True: fileName=input('請輸入要打開的文件名presidential_polls_clinton_state_mean_lable.csv:') try: df_count=pd.read_csv(fileName,encoding='cp936') df_count=df_count.columns['Label'] df_count=df_count.value_counts() print(df_count) plt.figure() count_cut_counts.plot(kind='bar',figsize=(12,8)) plt.plot(df_count['Label'],df_count['個數'],label='個數',color='blue') plt.xlabel("Label",fontsize=12)#設置x軸標簽 plt.ylabel("個數")#設置y軸標簽 plt.xticks(OneSup, TwoSup, ThreeSup, FourSup)#設置x軸刻度 plt.title('列Lable的個數統計柱狀圖') plt.legend(figsize=16)#顯示圖例並設置字號 plt.savefig('presidential_polls_clinton_state_support.png',dpi=300) plt.show() print("任務6執行成功!") break except Exception as e: print(e)dataDescribeVisualization()import pandas as pdimport matplotlib.pyplot as pltimport osplt.rcParams['font.sans-serif']=['SimHei']plt.rcParams['axes.unicode_minus']=Falsedef dataDescribeVisualization(): while True: fileName=input('請輸入要打開的文件名presidential_polls_clinton_state_mean_lable.csv:') try: df_count=pd.read_csv(fileName,encoding='cp936') df_count=df_count.columns['Label'] df_count=df_count.value_counts() print(df_count) plt.figure() count_cut_counts.plot(kind='bar',figsize=(12,8)) plt.plot(df_count['Label'],df_count['個數'],label='個數',color='blue') plt.xlabel("Label",fontsize=12)#設置x軸標簽 plt.ylabel("個數")#設置y軸標簽 plt.xticks(OneSup, TwoSup, ThreeSup, FourSup)#設置x軸刻度 plt.title('列Lable的個數統計柱狀圖') plt.legend(figsize=16)#顯示圖例並設置字號 plt.savefig('presidential_polls_clinton_state_support.png',dpi=300) plt.show() print("任務6執行成功!") break except Exception as e: print(e)dataDescribeVisualization()
報錯:only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved