本文主要是介绍Pandas-数据操作-字符串型(一):常用方法【str(自动过滤NaN值)、索引】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Pandas针对字符串配备的一套方法,使其易于对数组的每个元素进行操作。
一、str:通过str访问,且自动排除丢失/ NA值
通过str访问,且自动排除丢失/ NA值
- 直接通过.str调用字符串方法
- 可以对Series、Dataframe使用
- 自动过滤NaN值
import numpy as np
import pandas as pd# 通过str访问,且自动排除丢失/ NA值
# 直接通过.str调用字符串方法
# 可以对Series、Dataframe使用
# 自动过滤NaN值s = pd.Series(['A', 'b', 'C', 'bbhello', '123', np.nan, 'hj'])
df = pd.DataFrame({'key1': list('abcdef'),'key2': ['hee', 'fv', 'w', 'hija', '123', np.nan]})
print("s = \n", s)
print('-' * 50)
print("df = \n", df)
print('-' * 200)print("s.str.count('b') = \n", s.str.count('b'))
print('-' * 50)
print("df['key2'].str.upper() = \n", df['key2'].str.upper())
print('-' * 200)# df.columns是一个Index对象,也可使用.str
df.columns = df.columns.str.upper()
print("df = \n", df)
print('-' * 200)
打印结果:
s =
0 A
1 b
2 C
3 bbhello
4 123
5 NaN
6 hj
dtype: object
--------------------------------------------------
df = key1 key2
0 a hee
1 b fv
2 c w
3 d hija
4 e 123
5 f NaN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
s.str.count('b') = 0 0.0
1 1.0
2 0.0
3 2.0
4 0.0
5 NaN
6 0.0
dtype: float64
--------------------------------------------------
df['key2'].str.upper() = 0 HEE
1 FV
2 W
3 HIJA
4 123
5 NaN
Name: key2, dtype: object
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
df = KEY1 KEY2
0 a hee
1 b fv
2 c w
3 d hija
4 e 123
5 f NaN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Process finished with exit code 0
二、字符串索引
import numpy as np
import pandas as pd# 字符串索引s = pd.Series(['A', 'b', 'C', 'bbhello', '123', np.nan, 'hj'])
df = pd.DataFrame({'key1': list('abcdef'),'key2': ['hee', 'fv', 'w', 'hija', '123', np.nan]})# 取第一个字符
data1 = s.str[0]
print("取第一个字符: data1 = s.str[0] = \n", data1)
print('-' * 200)
# 取前两个字符
data2 = s.str[:2]
print("取前两个字符: data2 = s.str[:2] = \n", data2)
print('-' * 200)# str之后和字符串本身索引方式相同
data3 = df['key2'].str[:2]
print("data3 = df['key2'].str[:2] = \n", data3)
print('-' * 200)
打印结果:
取第一个字符: data1 = s.str[0] = 0 A
1 b
2 C
3 b
4 1
5 NaN
6 h
dtype: object
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
取前两个字符: data2 = s.str[:2] = 0 A
1 b
2 C
3 bb
4 12
5 NaN
6 hj
dtype: object
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
data3 = df['key2'].str[:2] = 0 he
1 fv
2 w
3 hi
4 12
5 NaN
Name: key2, dtype: object
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Process finished with exit code 0
这篇关于Pandas-数据操作-字符串型(一):常用方法【str(自动过滤NaN值)、索引】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!