温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

pandas数据集的处理示例

发布时间:2021-08-23 11:19:01 来源:亿速云 阅读:140 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“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

以上是“pandas数据集的处理示例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI