来源:15 Python Snippets to Optimize your Data Science Pipeline

翻译:RankFan

15种Python片段去优化你的数据科学管道

为什么片段对于数据科学是重要的

在我的日常中,我经常处理许多同样的状况,主要是从加载 csv 文件到数据可视化。因此,为了流水线这个过程,我有兴趣去储存一些 code 片段, 在不同的情形下,加载csv文件到数据可视化是非常有帮助的。

在这篇短文中,我将分享15个Python片段去简化你不同的数据分析管道。

1. 通过 GLob 和 List 加载多个文件

import glob
import pandas as pd
csv_files = glob.glob("path/to/folder/with/csvs/*.csv")
dfs = [pd.read_csv(filename) for filename in csv_flies]

2. 得到列中的唯一值

import pandas as pd
df = pd.read_csv("path/to/csv/file.csv")
df = ["Item_Identifier"].unique() array['FDA15', 'DRC01', 'FDN15', ..., 'NCF55', 'NCW30', 'NCW05'],dtype = object]

3. 并排展示Pandas Dataframe

from IPython.display import display_html
from itertools import chain, cycle def display_side_by_side(*arg, title = cycle([''])):
html_str = ""
for df, title in zip(args, chain(title, cycle(['</br>']))):
html_str += '< the style = " text-align : center "> < td style = "vertical-align : top">'
html_str += "<br>"
html_str += f'<h2>{title}</h2>'
html_str += df.to_html().replace('table', tabel style="display:inline")
html_str += '</td></th>' display_html(html_str, raw = True) df1 = pd.csv_read("file_csv")
df2 = pd.csv_read("file2") dispaly_side_by_side(df1.head(), df2.head(), titles=[Sales, Advertising])

4. 移除Pandas DataFrame中的缺失值

df = pd.DataFrame(dict(a = [1, 2, 3, None]))
df
df.dropna(inplace = True)
df

5. 显示缺失值的个数

def FindNanCol(df):
for col in df:
print(f"Column : {col}")
num_Nans = df[col].isnull().sum()
print(f"Number of Nans : {num_Nans}") df = pd.DataFrame(dict(a = [1, 2, 3, None], b = [None, None, 5, 6]))
FindNanCol(df)

6. 使用.apply 函数和 lambda 函数 转变列

df = pd.DataFrame(dict(a = [10, 20 ,30, 40, 50]))
square = lambda x: x**2
df["a"] = df["a"].apply(square)
df

7. 将两个DataFrame列转化为字典

df = pd.DataFrame(dict(a = ["a", "b", "c"], b = [1, 2, 3]))
df_dictionary = dict(zip(df["a"], df["b"]))
df_dictionary

8. 绘制列的网格分布

import numpy as np
import matplotlib.pyplot as plt
import seaborns as sns
import pandas as pd
sns.set() df = pd.DataFrame(dict(a = np.random.randint(0, 100, 100), b = np.arange(0, 100, 1)))
plt.figure(figsize = (15,7))
plt.subplot(1, 2, 1)
df["b"][df["a"]>50].hist(color='green', label="bigger than 50")
plt.legend() plt.subplot(1, 2, 1)
df["b"][df["a"]<50].hist(color='orange', label="small than 50")
plt.legend()
plt.show

9. 在pandas中对不同的列进行t检验

from scipy.stats import ttest_rel

data = np.arange(0, 1000, 1)
data_plus_noise = np.arange(0, 1000, 1) + np.random.normal(0, 1, 1000)
df = pd.DataFrame(dict(data = data, data_plus_noise = data_plus_noise))
print(ttest_rel(df["data"], df["data_plus_noise"]))

10. 合并数据

df1 = pd.DataFrame(dict(a = [1, 2, 3], b=[10, 20, 30], col_to_merge= ["a", "b", "c"]))
df2 = pd.DataFrame(dict(d = [10, 20, 30], col_to_merge=["a", "b", "c"]))
df_merged = df1.merge(df2, on='col_to_merge')

11. 用sklearn进行标准化

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scores = scaler.fit_transform(df["a"].values.reshape(-1, 1))

12. 丢弃特定列的缺失值

df.dropna(subset = ["col_to_remove_NaNs_from"], inplace = True)

13. 有条件的选择dataframe的子集

df = pd.Dataframe(dict(result = ["pass", "Fail", "pass", "Fail", "Distinction", "Distinction"]))
pass_index = (df["result"] == "pass") | (df["result"] == "Distinction")
df_pass = df['pass_index']
df_pass

14. 饼图

import matplotlib.pyplot as plt
df = pd.DataFrame(dict(a = [10, 20, 50, 10, 10], b=["A", "B", "C", "D", "E"]))
labels = df["b"]
sizes = df["a"]
plt.pie(sizes, labels = labels, autopct = '%1.1f%%', shadow = True, startangle=140)
plt.axis('equal')
plt.show

15. 将百分数字符串转化为数值

def change_to_numerical(x):
try:
x = int(x.strip("%")[:2])
except:
x = int(x.strip("%")[:1])
return x df = pd.DataFrame(dict(a =["A", "B" ,"C"], col_with_percentage = ["10%", "20%", "70%"]))
df["col_with_percentage"] = df["col_with_percentage"].apply(change_to_numerical)
df

结论

我认为代码片段是非常有用,重新写是浪费时间的,因此,有一个完整的工具包可以对数据分析进行流水线处理,这是非常有帮助的。

最新文章

  1. 基于 LocalAuthentication 框架的指纹解锁
  2. 新版Microsoft Azure Web管理控制台 - Microsoft Azure New Portal - (2)
  3. 使用while循环语句和变量输出九九乘法表
  4. MFC添加背景图片
  5. 【实践】纯jquery实现图片滑动无缝轮播,带左右按钮及控制按钮
  6. 表格类似Excel
  7. 724. Find Pivot Index
  8. jmeter-PC注册遇到的问题与解决
  9. Java对象、引用、实例
  10. Centos 配置eth0 提示Device does not seem to be present
  11. python简述
  12. 将表格导出为excel
  13. [math] 绘制空间几何体的直观图
  14. apache ab工具
  15. Node.js SDK与fabric链码交互开发
  16. 容易出错的 if 语句
  17. crond 的注意点
  18. class字节码结构(三)(字段集合的结构)
  19. c#:treeview双击某个节点的事件
  20. 在Spring Boot项目中使用Spock框架

热门文章

  1. C# 启动 Flask for Python
  2. 剑指offer面试题4
  3. VS code快速创建vue模板
  4. spring动态切换数据源(一)
  5. 使用什么快捷键,关闭、打开、最小化qq聊天窗口
  6. 用CUDA写出比Numpy更快的规约求和函数
  7. MySQL-后知知觉的索引
  8. VS Code 1.60 发布!竟然可以自动检测编程语言了!
  9. RabbitMQ-初见
  10. php open_basedir绕过