数据读取与数据分析(pandas)

数据读取与数据分析(pandas)

数据读取(利用pandas读取)

import pandas as pd
train_df = pd.read_csv('路径/train_set.csv', sep='\t', nrows=10000)

分隔符sep,为每列分割的字符,设置为\t即可;
读取行数nrows,为此次读取文件的函数,是数值类型(由于数据集比较大,建议先设置为10000);

数据分析

train_df[‘text_len’] =train_df[‘text’].apply(lambda x: len(x.split(’ ')))#以空格分割每个单词
print(train_df[‘text_len’].describe())


结果:

count 10000.000000
mean 908.766300
std 1033.708515
min 15.000000
25% 375.000000
50% 672.500000
75% 1123.000000
max 44665.000000
Name: text_len, dtype: float64

绘制直方图

import matplotlib.pyplot as plt#导入matplotlib.pyplot
_ = plt.hist(train_df['text_len'], bins=200)
plt.xlabel('Text char count')
plt.title("Histogram of char count")
plt.show()

结果:
在这里插入图片描述
新闻类别分布
train_df[‘label’].value_counts().plot(kind=‘bar’)
plt.title(‘News class count’)
plt.xlabel(“category”)
plt.plot()

字符分布统计

>>> from collections import Counter
>>> all_lines = ' '.join(list(train_df['text']))
>>> word_count = Counter(all_lines.split(" "))
>>> word_count = sorted(word_count.items(), key=lambda d:d[1], reverse = True)
>>> print(len(word_count))
5340
>>> print(word_count[0])
('3750', 373091)
>>> print(word_count[-1])
('6577', 1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值