Python data analysis: two-color ball statistics of which combination of red and blue balls is high, python Data Analysis

Source: Internet
Author: User

Python data analysis: two-color ball statistics of which combination of red and blue balls is high, python Data Analysis

This article describes how to calculate the ratio of two red and blue balls in a two-color ball statistical method based on Python data analysis. We will share this with you for your reference. The details are as follows:

Count the combination of two red and blue balls. The first 19 groups of data are displayed.

#! /Usr/bin/python #-*-coding: UTF-8-*-import pandas as pdimport numpy as npimport matplotlib. pyplot as pltimport operator # import data df = pd.read_table('newdata.txt ', header = None, sep =', ') tdate = sorted (df. loc [:, 0]) # print tdate # Red Ball h1 = df in the 1st and 2 columns. loc [:,]. values # print h1 # Red Ball h2 = df in columns 2nd and 3. loc [:, 2: 3]. values # 3rd, 4 columns of red ball h3 = df. loc [:, 3: 4]. values # 4th, 5 columns of red ball h4 = df. loc [:, 4: 5]. values # Red Ball h5 = df in columns 5th and 6. loc [:, 5: 6]. values # Blue ball b1 = df. loc [:, 7]. values # print b1 # 1st, 3 columns of red ball h6 = df. loc [:,]. valuesh7 = df. loc [:, :4:3]. valuesh8 = df. loc [:, :5:4]. valuesh9 = df. loc [:,]. valuesh10 = df. loc [:, :4:2]. valuesh11 = df. loc [:, :5:3]. valuesh12 = df. loc [:,]. valuesh13 = df. loc [:, :5:2]. valuesh14 = df. loc [:, :3 3]. values # 4th, 6 columns of red ball h15 = df. loc [:,]. values # Add the blue ball to each red ball group (two columns of data are changed to three columns of data), and then merge all data into data2 = np by column. append (h1, b1, axis = 1) for I in [h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13, h14, h15]: data1 = np. append (I, b1, axis = 1) data2 = np. append (data2, data1, axis = 0) print data2data1 = pd.dataframe(data21_1_write to data1.to_csv('2hldata.csv ', index = None, header = None) # Read files and make statistics, and sort f = open ("2hldata.csv") count_dict ={} for line in f. readlines (): line = line. strip () count = count_dict.setdefault (line, 0) count + = 1 count_dict [line] = countsorted_count_dict = sorted (count_dict.iteritems (), key = operator. itemgetter (1), reverse = True) # for item in sorted_count_dict: # print "% s, % d" % (item [0], item [1]) # reset indexfenzu = pd of DataFrame. dataFrame (sorted_count_dict ). set_index ([0]) print fenzux = list (fenzu. index [: 19]) y = list (fenzu. values [: 19]) print xprint y # Replace index with a numerical value to facilitate the use of s = pd for drawing. series (range (1, len (x) + 1), index = x) plt. figure (figsize = (12, 8), dpi = 80) plt. legend (loc = 'best') plt. bar (s, y, alpha =. 5, color = 'R', width = 0.8) plt. title ('the two red and one blue ball number') plt. xlabel ('two red and one blue number') plt. ylabel ('times ') # display the original index content plt. xticks (s, x, rotation = 30, size = 10, ha = 'left') plt. show ()

Display result:

We can see that the red balls 20, 26, and 9, and the red balls 17, 21, and 14 appear up to 12 times.

The following figure shows the statistics of three red and blue balls, four red and blue balls, five red and blue balls, and six red and blue balls. The basic idea is the same.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.