日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

【数据科学】使用Python建立你的数据处理肌肉记忆

發(fā)布時(shí)間:2024/3/13 python 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【数据科学】使用Python建立你的数据处理肌肉记忆 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)據(jù)預(yù)處理流程

?

在搜索語法時(shí),您是否因?yàn)槠茐臄?shù)據(jù)分析流而感到沮喪?為什么你在第三次查找之后仍然不記得它?這是因?yàn)槟氵€沒有足夠的練習(xí)來為它建立肌肉記憶。

現(xiàn)在,想象一下,當(dāng)您編寫代碼時(shí),Python語法和函數(shù)會根據(jù)您的分析思路從指尖飛出。那太棒了!本教程旨在幫助您實(shí)現(xiàn)目標(biāo)。

我建議每天早上練習(xí)這個(gè)劇本10分鐘,并重復(fù)一個(gè)星期。這就像每天做一些小小的仰臥起坐 - 不是為了你的腹肌,而是為了你的數(shù)據(jù)科學(xué)肌肉。逐漸地,您會注意到重復(fù)培訓(xùn)后數(shù)據(jù)分析編程效率的提高。

從我的“數(shù)據(jù)科學(xué)訓(xùn)練”開始,在本教程中,我們將練習(xí)最常用的數(shù)據(jù)預(yù)處理語法作為預(yù)熱會話;)

?

目錄

0.讀取,查看和保存數(shù)據(jù)

1。表的維度和數(shù)據(jù)類型

1.1尺寸

1.2數(shù)據(jù)類型

2.基本列操作

2.1按列子集數(shù)據(jù)

2.2重命名列

3.空值:查看,刪除和估算

3.1有多少行和列有空值?

3.2為固定的一組列選擇非空行

3.3 Null值的子集行

3.4刪除和估算缺失值

4.重復(fù)數(shù)據(jù)刪除

?

0.讀取,查看和保存數(shù)據(jù)

首先,為我們的練習(xí)加載庫:

# 1.Load libraries # import pandas as pd import numpy as np

現(xiàn)在我們將從我的GitHub存儲庫中讀取數(shù)據(jù)。我從Zillow下載了數(shù)據(jù)。

file_dir = "https://raw.githubusercontent.com/zhendata/Medium_Posts/master/City_Zhvi_1bedroom_2018_05.csv"# read csv file into a Pandas dataframe raw_df = pd.read_csv(file_dir)# check first 5 rows of the file raw_df.head(5)# use raw_df.tail(5) to see last 5 rows of the file

?

結(jié)果如下:

保存文件是dataframe.to_csv()。如果您不想保存索引號,請使用dataframe.to_csv(index = False)。

?

1。表的維度和數(shù)據(jù)類型

1.1尺寸

這個(gè)數(shù)據(jù)中有多少行和列?

?

raw_df.shape # the results is a vector: (# of rows, # of cols)# Get the number of rows print(raw_df.shape[0]) # column is raw_df.shape[1]

?

1.2數(shù)據(jù)類型

您的數(shù)據(jù)的數(shù)據(jù)類型是什么,有多少列是數(shù)字的?

# Check the data types of the entire table's columns raw_df.dtypes# Check the data type of a specific column raw_df['RegionID'].dtypes # result: dtype('int64')

輸出前幾列的數(shù)據(jù)類型:

如果您想更加具體地了解數(shù)據(jù),請使用select_dtypes()來包含或排除數(shù)據(jù)類型。問:如果我只想查看2018的數(shù)據(jù),我該如何獲得?

?

2.基本列操作

2.1按列子集數(shù)據(jù)

按數(shù)據(jù)類型選擇列:

# if you only want to include columns of float data raw_df.select_dtypes(include=['float64']) # Or to get numerical columns by excluding objects (non-numeric) raw_df.select_dtypes(exclude=['object'])# Get a list of all numerical column names # num_cols = raw_df.select_dtypes(include=[np.number]).columns.tolist()

例如,如果您只想要float和integer列:

按名稱選擇和刪除列:

# select a subset of columns by names raw_df_info = raw_df[['RegionID', 'RegionName', 'State', 'Metro', 'CountyName']]# drop columns by names raw_df_sub = raw_df_info.drop(['RegionID','RegionName'],axis=1) raw_df_sub.head(5)

?

2.2重命名列

如果我不喜歡它們,如何重命名列?例如,將“State”更改為“state_”;?'城市'到'city_':

# Change column names # raw_df_renamed1 = raw_df.rename(columns= {'State':'state_', 'City':'city_})# If you need to change a lot of columns: this is easy for you to map the old and new names old_names = ['State', 'City'] new_names = ['state_', 'city_'] raw_df_renamed2 = raw_df.rename(columns=dict(zip(old_names, new_names))

?

3.空值:查看,刪除和估算

3.1有多少行和列有空值?

# 1. For each column, are there any NaN values? raw_df.isnull().any()# 2. For each column, how many rows are NaN? raw_df.isnull().sum() # the results for 1&2 are shown in the screenshot below this block# 3. How many columns have NaNs? raw_df.isnull().sum(axis=0).count() # the result is 271. # axis=0 is the default for operation across rows, so raw_df.isnull().sum().count() yields the same result# 4. Similarly, how many rows have NaNs? raw_df.isnull().sum(axis=1).count() # the result is 1324

isnull.any()與isnull.sum()的輸出:

選擇一列中不為空的數(shù)據(jù),例如,“Metro”不為空。

raw_df_metro = raw_df[pd.notnull(raw_df['Metro'])] # If we want to take a look at what cities have null metros raw_df[pd.isnull(raw_df['Metro'])].head(5)

?

3.2為固定的一組列選擇非空行

選擇2000之后沒有null的數(shù)據(jù)子集:

如果要在7月份選擇數(shù)據(jù),則需要找到包含“-07”的列。要查看字符串是否包含子字符串,可以在字符串中使用子字符串,并輸出true或false。

# Drop NA rows based on a subset of columns: for example, drop the rows if it doesn't have 'State' and 'RegionName' info df_regions = raw_df.dropna(subset = ['State', 'RegionName']) # Get the columns with data available after 2000: use <string>.startwith("string") function # cols_2000= [x for x in raw_df.columns.tolist() if '2000-' in x] raw_df.dropna(subset=cols_2000).head(5)

?

3.3 Null值的子集行

選擇我們希望擁有至少50個(gè)非NA值的行,但不需要特定于列:

# Drop the rows where at least one columns is NAs. # Method 1: raw_df.dropna() #It's the same as df.dropna(axis='columns', how = 'all')# Method 2: raw_df[raw_df.notnull()]# Only drop the rows if at least 50 columns are Nas not_null_50_df = raw_df.dropna(axis='columns', thresh=50)

?

3.4刪除和估算缺失值

填寫NA或估算NA:

#fill with 0: raw_df.fillna(0)#fill NA with string 'missing': raw_df['State'].fillna('missing')#fill with mean or median: raw_df['2018-01'].fillna((raw_df['2018-01'].mean()),inplace=True) # inplace=True changes the original dataframe without assigning it to a column or dataframe # it's the same as raw_df['2018-01']=raw_df['2018-01'].fillna((raw_df['2018-01'].mean()),inplace=False)

使用where函數(shù)填充自己的條件:

# fill values with conditional assignment by using np.where # syntax df['column_name'] = np.where(statement, A, B) # # the value is A is the statement is True, otherwise it's B # # axis = 'columns' is the same as axis =1, it's an action across the rows along the column # axis = 'index' is the same as axis= 0; raw_df['2018-02'] = np.where(raw_df['2018-02'].notnull(), raw_df['2018-02'], raw_df['2017-02'].mean(), axis='columns')

?

4.重復(fù)數(shù)據(jù)刪除

在匯總數(shù)據(jù)或加入數(shù)據(jù)之前,我們需要確保沒有重復(fù)的行。

我們想看看是否有任何重復(fù)的城市/地區(qū)。我們需要確定我們想要在分析中使用哪個(gè)唯一ID(城市,地區(qū))。

# Check duplicates # raw_df.duplicated() # output True/False values for each columnraw_df.duplicated().sum() # for raw_df it's 0, meaning there's no duplication# Check if there's any duplicated values by column, output is True/False for each row raw_df.duplicated('RegionName')# Select the duplicated rows to see what they look like # keep = False marks all duplicated values as True so it only leaves the duplicated rows raw_df[raw_df['RegionName'].duplicated(keep=False)].sort_values('RegionName')

IF set keep = False:

刪除重復(fù)的值。

'CountyName'和'SizeRank'組合已經(jīng)是唯一的。所以我們只使用列來演示drop_duplicated的語法。

# Drop duplicated rows # # syntax: df.drop_duplicates(subset =[list of columns], keep = 'first', 'last', False) unique_df = raw_df.drop_duplicates(subset = ['CountyName','SizeRank'], keep='first')

?

這就是我在Python系列中為數(shù)據(jù)科學(xué)構(gòu)建肌肉記憶系列的第一部分。完整的腳本可以在這里找到。

?

原文:https://medium.freecodecamp.org/how-to-build-up-your-muscle-memory-for-data-science-with-python-5960df1c930e

?

?

總結(jié)

以上是生活随笔為你收集整理的【数据科学】使用Python建立你的数据处理肌肉记忆的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。