溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

pandas數(shù)據(jù)集的處理示例

發(fā)布時間:2021-08-23 11:19:01 來源:億速云 閱讀:143 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“pandas數(shù)據(jù)集的處理示例”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“pandas數(shù)據(jù)集的處理示例”這篇文章吧。

1. 數(shù)據(jù)集基本信息

df = pd.read_csv()

df.head():前五行;

df.info():

  • rangeindex:行索引;

  • data columns:列索引;

  • dtypes:各個列的類型,

  • 主體部分是各個列值的情況,比如可判斷是否存在 NaN 值;

對于非數(shù)值型的屬性列

  • df[‘some_categorical_columns'].value_counts():取值分布;

df.describe(): 各個列的基本統(tǒng)計信息

  • count

  • mean

  • std

  • min/max

  • 25%, 50%, 75%:分位數(shù)

df.hist(bins=50, figsize=(20, 15)):統(tǒng)計直方圖;

對 df 的每一列進(jìn)行展示:

train_prices = pd.DataFrame({'price': train_df.SalePrice, 
    'log(price+1)': np.log1p(train_df.SalePrice)})
 # train_prices 共兩列,一列列名為 price,一列列名為 log(price+1)
train_prices.hist()

2. 數(shù)據(jù)集拆分

def split_train_test(data, test_ratio=.3):
 shuffled_indices = np.random.permutation(len(data))
 test_size = int(len(data)*test_ratio)
 test_indices = shuffled_indices[:test_size]
 train_indices = shuffled_indices[test_size:]
 return data.iloc[train_indices], data.iloc[test_indices]

3. 數(shù)據(jù)預(yù)處理

  • 一鍵把 categorical 型特征(字符串類型)轉(zhuǎn)化為數(shù)值型:

>> df['label'] = pd.Categorical(df['label']).codes
  • 一鍵把 categorical 型特征(字符串類型)轉(zhuǎn)化為 one-hot 編碼:

>> df = pd.get_dummies(df)
  • null 值統(tǒng)計與填充:

>> df.isnull().sum().sort_values(ascending=False).head()
# 填充為 mean 值
>> mean_cols = df.mean()
>> df = df.fillna(mean_cols)
>> df.isnull().sum().sum()
0

以上是“pandas數(shù)據(jù)集的處理示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI