加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Python数据分析-看了这篇文章,数据清洗你也就完全掌握了

发布时间:2019-09-12 02:49:19 所属栏目:教程 来源:哗啦圈的梦
导读:所有做数据分析的前提就是:你得有数据,而且已经经过清洗,整理成需要的格式。 不管你从哪里获取了数据,你都需要认真仔细观察你的数据,对不合规的数据进行清理,虽然不是说一定要有这个步骤,但是这是一个好习惯,因为保不齐后面分析的时候发现之前因为

2、append

  1. 1.result = df1.append(df2) 
  2. 2.result = df1.append(df4) 
  3. 3.result = df1.append([df2, df3]) 
  4. 4.result = df1.append(df4, ignore_index=True) 

4、join

left.join(right, on=key_or_keys)

  1. 1.result = left.join(right, on='key') 
  2. 2.result = left.join(right, on=['key1', 'key2']) 
  3. 3.result = left.join(right, on=['key1', 'key2'], how='inner') 

5、concat

  1. 1.result = pd.concat([df1, df4], axis=1) 
  2. 2.result = pd.concat([df1, df4], axis=1, join='inner') 
  3. 3.result = pd.concat([df1, df4], axis=1, join_axes=[df1.index]) 
  4. 4.result = pd.concat([df1, df4], ignore_index=True) 

文本处理:

1. lower()函数示例

  1. s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveMinsu']) 
  2. s.str.lower() 

2. upper()函数示例

  1. s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveMinsu']) 
  2. s.str.upper() 

3. len()计数

  1. s = pd.Series(['Tom', 'William Rick', 'John', 'Alber@t', np.nan, '1234','SteveMinsu']) 
  2. s.str.len() 

4. strip()去除空格

  1. s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 
  2. s.str.strip() 

5. split(pattern)切分字符串

  1. s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 
  2. s.str.split(' ') 

6. cat(sep=pattern)合并字符串

  1. s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 
  2. s.str.cat(sep=' <=> ') 
  3. 执行上面示例代码,得到以下结果 - 
  4. Tom <=> William Rick <=> John <=> Alber@t 

7. get_dummies()用sep拆分每个字符串,返回一个虚拟/指示dataFrame

  1. s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 
  2. s.str.get_dummies() 

8. contains()判断字符串中是否包含子串true; pat str或正则表达式

  1. s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 
  2. s.str.contains(' ') 

9. replace(a,b)将值pat替换为值b。

  1. s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 
  2. .str.replace('@','$') 

10. repeat(value)重复每个元素指定的次数

  1. s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 
  2. s.str.repeat(2) 

执行上面示例代码,得到以下结果 -

  • 0 Tom Tom
  • 1 William Rick William Rick
  • 2 JohnJohn
  • 3 Alber@tAlber@t

11. count(pattern)子串出现次数

  1. s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 
  2. print ("The number of 'm's in each string:") 
  3. print (s.str.count('m')) 

执行上面示例代码,得到以下结果 -

The number of 'm's in each string:

  • 0 1
  • 1 1
  • 2 0
  • 3 0

12. startswith(pattern)字符串开头是否匹配子串True

  1. s = pd.Series(['Tom ', ' William Rick', 'John', 'Alber@t']) 
  2. print ("Strings that start with 'T':") 
  3. print (s.str. startswith ('T')) 

执行上面示例代码,得到以下结果 -

Strings that start with 'T':

  • 0 True
  • 1 False
  • 2 False
  • 3 False

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读