美文网首页
16 Python and Pandas Hacks

16 Python and Pandas Hacks

作者: Python_Camp | 来源:发表于2022-06-15 18:46 被阅读0次

16 Python and Pandas Hacks that will save you time in your project
Make your Data Science workflow more efficient
https://youtu.be/oR670Txwh88
Free for Use photo from Pexels

  1. Check memory usage of your objects
    检查你的对象的内存使用情况
import sys
lst = range(0, 500)
print(sys.getsizeof(lst))

  1. Converting string to byte
    将字符串转换为字节
s = “I want to convert this string to byte”
s.encode( )

# b’I want to convert this string to byte’
  1. Merge two list objects
    合并两个列表对象
a = [1,2,3]
b = [10,20,30]
a.extend(b)
# [1,2,3,10,20,30]
  1. 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’
  1. Count the number of occurrences of a character in a string
    计算一个字符串中出现的字符的数量
"please find the occurrence of a in this sentence'.count('a')
# 2
  1. 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'
  1. 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]

  1. 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)]
  1. Thousands Separator
    千位数分隔符
N = 5384200
comma_separated_N = f‘{N: , 1}’
comma_separated_N
#5,384,200
  1. 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
  1. Get quotient and remainder
    获取商和余数
q, r= divmod(25,6)
# q, r= 4, 1
  1. 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

  1. 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])) 

new_d = {k:v for k,v in lst}
new_d 
# {“STATO”:0, “AN”:5, “HA”:5, “AK”:10, “HEW”:10}

首先根据值排序,然后根据键的长度排序,最后根据键本身排序(所以在本例中是按字母顺序排序)

  1. 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]]
  1. Use Switch Statement for Python 3.10
    使用Python 3.10的Switch语句
    参考https://docs.python.org/3.10/whatsnew/3.10.html
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.

  1. Loading multiple pandas dataframes onto each element of list
    在列表的每个元素上加载多个 pandas 数据框
import glob
import pandas as pd
files= glob.glob({path})
df_list= [pd.read_csv(file) for file in files]

  1. Insert Image in R markdown or Jupyter Notebook
    在R的markdown或Jupyter笔记本中插入图片
# 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

相关文章

网友评论

      本文标题:16 Python and Pandas Hacks

      本文链接:https://www.haomeiwen.com/subject/wdwxvrtx.html