Some tips for sharing Python data statistics _python

Source: Internet
Author: User

Recently in Python data statistics, summed up some of the recent use of some tips to find and summarize, hoping to help in doing this in some children's shoes. Some of the techniques are common and we don't pay attention to them at ordinary times, but in a particular scenario, these small methods can be a great help.

1. Mapping keys to multiple values in a dictionary

{' B ': [4, 5, 6], 
' a ': [1, 2, 3]}

Sometimes when we're counting the same key values, we want to add all the same key entries to a dictionary with key keys, and then we can do all sorts of things, and then we'll use the following code:

From collections import Defaultdict
d = defaultdict (list)
print (d)
d[' A '].append (1)
d[' A '].append (2 )
d[' A '].append (3)
d[' B '].append (4)
d[' B '].append (5)
d[' B '].append (6)
print (d)
Print (D.get ("a")) Print (
D.keys ())
print ([D.get (i) for i in D])

Here is the method used in the collections, which also has a lot of useful methods, we have time to continue to do in-depth understanding.

The above code runs the result:

Defaultdict (, {})
defaultdict (, {' B ': [4, 5, 6], ' A ': [1, 2, 3]})
[1, 2, 3]
dict_keys ([' B ', ' a '])
[[4] 5, 6], [1, 2, 3]]

After we fill in the data, it's the equivalent of a quick grouping, and then we iterate through each group to count the data we need.

2. Rapid conversion Dictionary key value pair

Data = {...}
Zip (Data.values (), Data.keys ())

Data is our format, use zip for fast key-value conversion, and then use functions like max,min to manipulate data.

3. Sort the dictionary by public key

From operator import itemgetter
data = [
  {' name ': ' Bran ', ' uid ': ', '
  {' name ': ' Xisi ', ' UID ': 102},
  {' Na ' Me ': Land ', "UID": "Sorted"
print (data, Key=itemgetter ("name"))
print (data, sorted, key= Itemgetter ("UID"))

The format of the data, which we want to sort the name or UID, is the method in the code.
Run Result:

[{' Name ': ' Bran ', ' uid ': #}, {' name ': ' Land ', ' uid ': ' n ', {' name ': ' Xisi ', ' uid ': ' 102}]
[{' Name ': ' Bran ', ' uid ': #}, {' name ': ' Xisi ', ' UID ': 102}, {' name ': ' Land ', ' UID ': 103}]

Just like we expected.

4. To group multiple dictionaries in a list according to a field

Note that the data is sorted first before grouping, and the sort field is selected according to the actual requirements

Data about to be processed:

rows = [
  {' name ': ' Bran ', ' uid ': ', ' class ': '} ',
  {' name ': ' Xisi ', ' uid ': ', ' class ': one},
  {' name ': ' Land ' , "UID": "The", "Class": ten}
]

Expected processing results:

{a
: [{' Name ': ' Xisi ', ' class ': One, ' uid ': 101},{' name ': ' Bran ', ' class ': ', ' uid ': '], '
[{' Name ']: ' Land ', ' class ': Ten, ' UID ':
}]}

We grouped by UID, and this is just a demo, and the UID doesn't usually repeat.

This is a little more complicated, and we break it down one step at a

some = [(' A ', [1, 2, 3]), (' B ', [4, 5, 6])]
print (Dict (some))

Results:

{' B ': [4, 5, 6], ' A ': [1, 2, 3]}

Our goal here is to convert tuples into dictionaries, which is very simple and should be read. Next, we'll sort through the processing data:

Data_one = sorted (rows, Key=itemgetter ("class"))
print (data_one)
data_two = sorted (rows, Key=lambda x: (x["UID "], x[" class "])
print (data_two)

Here we provide two kinds of sorting principle the same, but the style is slightly different, the first type of data_one is directly using Itemgetter, according to our previous use, directly according to a field to sort, but sometimes we will have another request:

Sort by a field first, when the first field repeats, and then by another field.

Then we use the second method to sort multiple field values.
The results of the sorting are as follows:

[{' Name ': ' Land ', ' class ': Ten, ' uid ': ' n ', {' name ': ' Xisi ', ' class ': One, ' uid ': ' n ', {' name ': ' Bran ', ' class ': ', ' uid ' :
[{'] [{' Name ': ' Xisi ', ' class ': One, ' uid ': +}, {' name ': ' Bran ', ' class ': ', ' uid ': ' ' name ': ' Land ', ' class ': ' UID ': 103}]

As a result, we take a look at it slowly, or slightly different.

The next step is to combine the two approaches we've just talked about:

data = Dict ([(G, List (k)) for G, K in GroupBy (Data_two, Key=lambda x:x["UID"])
print (data)

We grouped the sorted data, then we generated the tuple list, and finally we converted it to a dictionary, and here we are, we successfully grouped the data.

Python data statistics Some of the tips to share this, there is a need to reference learning.

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.