avatarSeungjun (Josh) Kim

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4144

Abstract

number">2</span>,<span class="hljs-number">3</span>]</pre></div><div id="fc42"><pre><span class="hljs-function"><span class="hljs-title">list</span><span class="hljs-params">(combinations(lst,<span class="hljs-number">2</span>)</span></span>)</pre></div><div id="ab6a"><pre><span class="hljs-attr"># [[(1</span>,<span class="hljs-number">2</span>,), <span class="hljs-comment">(1,3)</span>, <span class="hljs-comment">(2,3)</span>]</pre></div><div id="63ce"><pre><span class="hljs-function"><span class="hljs-title">list</span><span class="hljs-params">(combinations_with_replacement(lst,<span class="hljs-number">2</span>)</span></span>)</pre></div><div id="ef1e"><pre><span class="hljs-attr"># [[(1</span>,<span class="hljs-number">1</span>),<span class="hljs-comment">(1,2)</span>,<span class="hljs-comment">(1,3)</span>,<span class="hljs-comment">(2,2)</span>,<span class="hljs-comment">(2,3)</span>,<span class="hljs-comment">(3,3)</span>]</pre></div><div id="256b"><pre><span class="hljs-function"><span class="hljs-title">list</span><span class="hljs-params">(combinations(lst,<span class="hljs-number">3</span>)</span></span>)</pre></div><div id="49f7"><pre><span class="hljs-meta">#[(1,2,3)]</span></pre></div><h2 id="12fc">9. Thousands Separator</h2><div id="b950"><pre><span class="hljs-attribute">N</span> <span class="hljs-operator">=</span> <span class="hljs-number">5384200</span></pre></div><div id="c942"><pre><span class="hljs-attr">comma_separated_N</span> = f‘{N: , <span class="hljs-number">1</span>}’</pre></div><div id="72c7"><pre>comm<span class="hljs-built_in">a_separated</span>_N</pre></div><div id="5dd7"><pre><span class="hljs-meta">#5,384,200</span></pre></div><h2 id="523b">10. Sum or Multiply all elements of a list</h2><div id="cf4a"><pre><span class="hljs-keyword">from</span> numpy <span class="hljs-keyword">import</span> prod</pre></div><div id="e739"><pre><span class="hljs-keyword">from</span> functools <span class="hljs-keyword">import</span> reduce</pre></div><div id="c8ac"><pre><span class="hljs-attr">lst</span> = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]</pre></div><div id="e540"><pre><span class="hljs-attr">p1</span> = prod(lst) <span class="hljs-comment">#24</span></pre></div><div id="1d7a"><pre><span class="hljs-attr">p2</span> = reduce((lambda x, y: x * y), lst) <span class="hljs-comment">#24</span></pre></div><div id="0ef1"><pre><span class="hljs-attr">s</span> = sum(lst) <span class="hljs-comment">#10</span></pre></div><h2 id="ae20">11. Get quotient and remainder</h2><div id="53e1"><pre><span class="hljs-attribute">q</span>, r= divmod(<span class="hljs-number">25</span>,<span class="hljs-number">6</span>)</pre></div><div id="54ff"><pre><span class="hljs-meta"># q, r= 4, 1</span></pre></div><h2 id="46c7">12. Most frequent element of a list</h2><div id="9901"><pre>lst = [<span class="hljs-number">1,2,3,1</span>,<span class="hljs-number">1,1,10,20</span>,<span class="hljs-number">3,4,5,6</span>]</pre></div><div id="0460"><pre>most_freq_e = <span class="hljs-built_in">Counter</span>(lst)<span class="hljs-selector-class">.most_common</span>()<span class="hljs-selector-attr">[0]</span><span class="hljs-selector-attr">[0]</span> </pre></div><div id="6707"><pre><span class="hljs-meta">#1</span></pre></div><h2 id="ed25">13. Sort Dictionary based on multiple conditions</h2><div id="d627"><pre><span class="hljs-attribute">d</span> = {“HA”:<span class="hljs-number">5</span>, “AN”:<span class="hljs-number">5</span>, “AK”:<span class="hljs-number">10</span>, “STATO”:<span class="hljs-number">0</span> , “HEW”:<span class="hljs-number">10</span>}</pre></div><div id="9b2a"><pre>lst = <span class="hljs-built_in">sorted</span>(d<span class="hljs-selector-class">.items</span>( ), key = lamda x: (x<span class="hljs-selector-attr">[1]</span>, <span class="hljs-built_in">len</span>(x<span class="hljs-selector-attr">[0]</span>), x<span class="hljs-selector-attr">[0]</span>)) </pre></div><div id="5194"><pre>#### sorts based on value first, then length <span class="hljs-keyword">

Options

of</span> key and finally on the key itself (so alphabetically <span class="hljs-keyword">in</span> this <span class="hljs-keyword">case</span>) #####</pre></div><div id="5b1c"><pre><span class="hljs-keyword">new</span><span class="hljs-type">_d</span> = {k:<span class="hljs-type">v for k</span>,v <span class="hljs-keyword">in</span> lst}</pre></div><div id="24cc"><pre><span class="hljs-keyword">new</span><span class="hljs-type">_d</span> </pre></div><div id="9a7f"><pre><span class="hljs-meta"># {“STATO”:0, “AN”:5, “HA”:5, “AK”:10, “HEW”:10}</span></pre></div><h2 id="ab3b">14. Transpose a matrix</h2><p id="1746">mtrx= [[1,2,3], [3,4,5], [5,6,7]]</p><p id="0e25">lst = list(list(x) for x in zip(*mtrx))</p><p id="f901"># lst = [[1,3,5], [2,4,6], [3,6,7]]</p><h2 id="40b2">15. Use Switch Statement for Python 3.10</h2><p id="6b90">Refer to <a href="https://docs.python.org/3.10/whatsnew/3.10.html">https://docs.python.org/3.10/whatsnew/3.10.html</a></p><div id="db1f"><pre>def get_mood(<span class="hljs-keyword">day</span>): <span class="hljs-keyword">match</span> <span class="hljs-keyword">day</span>: <span class="hljs-keyword">case</span> ‘Monday’: <span class="hljs-keyword">return</span> ‘Oh…’ <span class="hljs-keyword">case</span> ‘Thursday’: <span class="hljs-keyword">return</span> ‘Getting <span class="hljs-keyword">close</span><span class="hljs-operator">!</span><span class="hljs-keyword">case</span> ‘Friday’: <span class="hljs-keyword">return</span> ‘Almost there<span class="hljs-operator">!</span><span class="hljs-keyword">case</span> ‘Saturday’ <span class="hljs-operator">|</span> ‘Sunday’: <span class="hljs-keyword">return</span> ‘Weekend<span class="hljs-operator">!</span><span class="hljs-operator">!</span><span class="hljs-operator">!</span><span class="hljs-keyword">case</span> _: <span class="hljs-keyword">return</span> ‘Meh…’</pre></div><p id="6e1b">Good <a href="https://towardsdatascience.com/switch-case-statements-are-coming-to-python-d0caf7b2bfd3">tutorial</a> on this.</p><h2 id="f2a5">16. Loading multiple pandas dataframes onto each element of list</h2><div id="586b"><pre><span class="hljs-keyword">import</span> glob <span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd</pre></div><div id="2a2e"><pre><span class="hljs-keyword">files</span>= <span class="hljs-built_in">glob</span>.<span class="hljs-built_in">glob</span>({path}) df_list= [pd.read_csv(<span class="hljs-keyword">file</span>) <span class="hljs-keyword">for</span> <span class="hljs-keyword">file</span> in <span class="hljs-keyword">files</span>]</pre></div><h2 id="866d">18. Insert Image in R markdown or Jupyter Notebook</h2><div id="0d71"><pre># <span class="hljs-keyword">Method</span> 1</pre></div><div id="a62d"><pre>! [Alt <span class="hljs-built_in">text</span>] (<span class="hljs-regexp">/Users/</span>Antonio/Documents/images/tufte.book.jpg)</pre></div><div id="18d4"><pre># <span class="hljs-keyword">Method</span> 2</pre></div><div id="997d"><pre>```{r pressure, <span class="hljs-attribute">echo</span>=<span class="hljs-literal">FALSE</span>, fig.<span class="hljs-attribute">cap</span>=”A caption”, <span class="hljs-attribute">message</span>=<span class="hljs-literal">FALSE</span>, <span class="hljs-attribute">warning</span>=<span class="hljs-literal">FALSE</span>, out.<span class="hljs-attribute">width</span>=, paged.<span class="hljs-attribute">print</span>=<span class="hljs-literal">FALSE</span>} knitr::include_graphics(“C:/Users/josh/Desktop/nn.jpg”)

16 Python and Pandas Hacks that will save you time in your project

Make your Data Science workflow more efficient

Free for Use photo from Pexels

1. Check memory usage of your objects

import sys
lst = range(0, 500)
print(sys.getsizeof(lst))

2. Converting string to byte

s = “I want to convert this string to byte”

s.encode( )

# b’I want to convert this string to byte’

3. Merge two list objects

a = [1,2,3]
b = [10,20,30]
a.extend(b)
# [1,2,3,10,20,30]

4. Retrieve longest and shortest string in a list

word_list = [“I”, “am”, “handsome”]
max(word_list, key=len)
# ‘handsome’
min(word_list,key=len)
# ‘I’

5. Count the number of occurrences of a character in a string

"please find the occurrence of a in this sentence'.count('a')
# 2

6. Unpack unequal number of values into variables

x, y, z* = ['a','b','c','d','e','f','g']
x 
# 'a'
y
# 'b'
z
# 'c','d','e','f','g'

7. Flatten a list

lst = [[1,2], [3,4], [5,6]]
flattened_lst = [i for j in list for i in j ]
lst = [1,2,3,4,5,6]

8. Print all combinations in a list

from itertools import combinations, combinations_with_replacement
lst= [1,2,3]
list(combinations(lst,2))
# [[(1,2,), (1,3), (2,3)]
list(combinations_with_replacement(lst,2))
# [[(1,1),(1,2),(1,3),(2,2),(2,3),(3,3)]
list(combinations(lst,3))
#[(1,2,3)]

9. Thousands Separator

N = 5384200
comma_separated_N = f‘{N: , 1}’
comma_separated_N
#5,384,200

10. Sum or Multiply all elements of a list

from numpy import prod
from functools import reduce
lst = [1,2,3,4]
p1 = prod(lst) #24
p2 = reduce((lambda x, y: x * y), lst) #24
s = sum(lst) #10

11. Get quotient and remainder

q, r= divmod(25,6)
# q, r= 4, 1

12. Most frequent element of a list

lst = [1,2,3,1,1,1,10,20,3,4,5,6]
most_freq_e = Counter(lst).most_common()[0][0] 
#1

13. Sort Dictionary based on multiple conditions

d = {“HA”:5, “AN”:5, “AK”:10, “STATO”:0 , “HEW”:10}
lst = sorted(d.items( ), key = lamda x: (x[1], len(x[0]), x[0])) 
#### sorts based on value first, then length of key and finally on the key itself (so alphabetically in this case) #####
new_d = {k:v for k,v in lst}
new_d 
# {“STATO”:0, “AN”:5, “HA”:5, “AK”:10, “HEW”:10}

14. Transpose a matrix

mtrx= [[1,2,3], [3,4,5], [5,6,7]]

lst = list(list(x) for x in zip(*mtrx))

# lst = [[1,3,5], [2,4,6], [3,6,7]]

15. Use Switch Statement for Python 3.10

Refer to https://docs.python.org/3.10/whatsnew/3.10.html

def get_mood(day):
    match day:
        case ‘Monday’:
            return ‘Oh…’
        case ‘Thursday’:
            return ‘Getting close!case ‘Friday’:
            return ‘Almost there!case ‘Saturday’ | ‘Sunday’:
            return ‘Weekend!!!case _:
            return ‘Meh…’

Good tutorial on this.

16. Loading multiple pandas dataframes onto each element of list

import glob
import pandas as pd
files= glob.glob({path})
df_list= [pd.read_csv(file) for file in files]

18. Insert Image in R markdown or Jupyter Notebook

# Method 1
! [Alt text] (/Users/Antonio/Documents/images/tufte.book.jpg)
# Method 2
```{r pressure, echo=FALSE, fig.cap=”A caption”, message=FALSE, warning=FALSE, out.width=, paged.print=FALSE}
knitr::include_graphics(“C:/Users/josh/Desktop/nn.jpg”)
```

About the Author

Data Scientist. Working as a research associate at the Criminal Justice Administrative Records System (CJARS) economics lab at the University of Michigan. Incoming PhD student in Informatics. He loves sports, working-out, cooking good Asian food, watching kdramas and making / performing music and most importantly worshiping Jesus Christ. Checkout his website!

Python
Data Science
Data
Projects
Recommended from ReadMedium