/
pandas_str_slice.py
79 lines (65 loc) · 1.24 KB
/
pandas_str_slice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pandas as pd
df = pd.DataFrame({'a': ['abcde', 'fghij', 'klmno'],
'b': [123, 456, 789]})
print(df)
# a b
# 0 abcde 123
# 1 fghij 456
# 2 klmno 789
print(df.dtypes)
# a object
# b int64
# dtype: object
print(df['a'].str[:2])
# 0 ab
# 1 fg
# 2 kl
# Name: a, dtype: object
print(df['a'].str[-2:])
# 0 de
# 1 ij
# 2 no
# Name: a, dtype: object
print(df['a'].str[::2])
# 0 ace
# 1 fhj
# 2 kmo
# Name: a, dtype: object
print(df['a'].str[2])
# 0 c
# 1 h
# 2 m
# Name: a, dtype: object
print(df['a'].str[0])
# 0 a
# 1 f
# 2 k
# Name: a, dtype: object
print(df['a'].str[-1])
# 0 e
# 1 j
# 2 o
# Name: a, dtype: object
df['a_head'] = df['a'].str[:2]
print(df)
# a b a_head
# 0 abcde 123 ab
# 1 fghij 456 fg
# 2 klmno 789 kl
# print(df['b'].str[:2])
# AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
print(df['b'].astype(str).str[:2])
# 0 12
# 1 45
# 2 78
# Name: b, dtype: object
print(df['b'].astype(str).str[:2].astype(int))
# 0 12
# 1 45
# 2 78
# Name: b, dtype: int64
print((df['b'] / 10).astype(int))
# 0 12
# 1 45
# 2 78
# Name: b, dtype: int64