pandas數據集的端到端處理

今天小編就爲大家分享一篇關於pandas數據集的端到端處理,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

1. 數據集基本信息

df = pd.read_csv()

df.head():前五行;

df.info():

  • rangeindex:行索引;
  • data columns:列索引;
  • dtypes:各個列的類型,
  • 主體部分是各個列值的情況,比如可判斷是否存在 NaN 值;

對於非數值型的屬性列

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

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

  • count
  • mean
  • std
  • min/max
  • 25%, 50%, 75%:分位數

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

對 df 的每一列進行展示:

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. 數據集拆分

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. 數據預處理

  • 一鍵把 categorical 型特徵(字符串類型)轉化爲數值型:
>> df['label'] = pd.Categorical(df['label']).codes
  • 一鍵把 categorical 型特徵(字符串類型)轉化爲 one-hot 編碼:
>> df = pd.get_dummies(df)
  • null 值統計與填充:
>> df.isnull().sum().sort_values(ascending=False).head()
# 填充爲 mean 值
>> mean_cols = df.mean()
>> df = df.fillna(mean_cols)
>> df.isnull().sum().sum()
0

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對神馬文庫的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章