Pandas之旅(六): 字串實用方法彙總
有關字串基本方法
大家好,我又回來了! 之前的幾期我們已經簡單瞭解了pandas的基礎操作,但是隻要涉及到資料,最常見的就是String(字串)型別,所以很多時候我們其實都在和字串打交道,所以今天,我會把我自己總結的,有關字串的常用方法分享給大家,希望能夠幫到各位小夥伴~
Split and format
latitude = '37.24N' longitude = '-115.81W' 'Coordinates {0},{1}'.format(latitude,longitude) >>>'Coordinates 37.24N,-115.81W'
f'Coordinates {latitude},{longitude}' >>>'Coordinates 37.24N,-115.81W'
'{0},{1},{2}'.format(*('abc')) >>>'a,b,c'
coord = {"latitude":latitude,"longitude":longitude} 'Coordinates {latitude},{longitude}'.format(**coord) >>>'Coordinates 37.24N,-115.81W'
Access argument' s attribute
class Point: def __init__(self,x,y): self.x,self.y = x,y def __str__(self): return 'Point({self.x},{self.y})'.format(self = self) def __repr__(self): return f'Point({self.x},{self.y})'
test_point = Point(4,2) test_point >>>Point(4,2)
str(Point(4,2)) >>>'Point(4,2)'
Replace with %s , %r:
" repr() shows the quote {!r}, while str() doesn't:{!s} ".format('a1','a2') >>> " repr() shows the quote 'a1', while str() doesn't:a2 "
Align:
'{:<30}'.format('left aligned') >>>'left aligned'
'{:>30}'.format('right aligned') >>>'right aligned'
'{:^30}'.format('centerd') >>>'centerd'
'{:*^30}'.format('centerd') >>>'***********centerd************'
Replace with %x , %o:
"int:{0:d}, hex:{0:x}, oct:{0:o}, bin:{0:b}".format(42) >>>'int:42, hex:2a, oct:52, bin:101010'
'{:,}'.format(12345677) >>>'12,345,677'
Percentage:
points = 19 total = 22 'Correct answers: {:.2%}'.format(points/total) >>>'Correct answers: 86.36%'
Date:
import datetime as dt f"{dt.datetime.now():%Y-%m-%d}" >>>'2019-03-27'
f"{dt.datetime.now():%d_%m_%Y}" >>>'27_03_2019'
today = dt.datetime.today().strftime("%d_%m_%Y") today
'27_03_2019'
Split without parameters:
"this is atest".split() >>>['this', 'is', 'a', 'test']
Concatenate:
'do'*2 >>>'dodo'
orig_string ='Hello' orig_string+',World' >>>'Hello,World'
full_sentence = orig_string+',World' full_sentence >>>'Hello,World'
Check string type , slice,count,strip:
strings = ['do','re','mi'] ', '.join(strings) >>>'do, re, mi'
'z' not in 'abc' >>> True
ord('a'), ord('#') >>> (97, 35)
chr(97) >>>'a'
s = "foodbar" s[2:5] >>>'odb'
s[:4] + s[4:] >>>'foodbar'
s[:4] + s[4:] == s >>>True
t=s[:] id(s) >>>1547542895336
id(t) >>>1547542895336
s is t >>>True
s[0:6:2] >>>'fob'
s[5:0:-2] >>>'ado'
s = 'tomorrow is monday' reverse_s = s[::-1] reverse_s >>>'yadnom si worromot'
s.capitalize() >>>'Tomorrow is monday'
s.upper() >>>'TOMORROW IS MONDAY'
s.title() >>>'Tomorrow Is Monday'
s.count('o') >>> 4
"foobar".startswith('foo') >>>True
"foobar".endswith('ar') >>>True
"foobar".endswith('oob',0,4) >>>True
"foobar".endswith('oob',2,4) >>>False
"My name is yo, I work at SG".find('yo') >>>11
# If can't find the string, return -1 "My name is ya, I work at Gener".find('gent') >>>-1
# Check a string if consists of alphanumeric characters "abc123".isalnum() >>>True
"abc%123".isalnum() >>>False
"abcABC".isalpha() >>>True
"abcABC1".isalpha() >>>False
'123'.isdigit() >>>True
'123abc'.isdigit() >>>False
'abc'.islower() >>>True
"This Is A Title".istitle() >>>True
"This is a title".istitle() >>>False
'ABC'.isupper() >>>True
'ABC1%'.isupper() >>>True
'foo'.center(10) >>>'foo'
'foo bar baz'.strip() >>>'foo bar baz'
'foo bar baz'.lstrip() >>>'foo bar baz'
'foo bar baz'.rstrip() >>>'foo bar baz'
"foo abc foo def foljk ".replace('foo','yao') >>>'yao abc yao def foljk '
'www.realpython.com'.strip('w.moc') >>>'realpython'
'www.realpython.com'.strip('w.com') >>>'realpython'
'www.realpython.com'.strip('w.ncom') >>>'realpyth'
Convert to lists:
', '.join(['foo','bar','baz','qux']) >>>'foo, bar, baz, qux'
list('corge') >>>['c', 'o', 'r', 'g', 'e']
':'.join('corge') >>>'c:o:r:g:e'
'www.foo'.partition('.') >>>('www', '.', 'foo')
'foo@@bar@@baz'.partition('@@') >>>('foo', '@@', 'bar@@baz')
'foo@@bar@@baz'.rpartition('@@') >>>('foo@@bar', '@@', 'baz')
'foo.bar'.partition('@@') >>>('foo.bar', '', '')
# By default , rsplit split a string with white space 'foo bar adf yao'.rsplit() >>>['foo', 'bar', 'adf', 'yao']
'foo.bar.adf.ert'.split('.') >>>['foo', 'bar', 'adf', 'ert']
'foo\nbar\nadfa\nlko'.splitlines() >>>['foo', 'bar', 'adfa', 'lko']
總結
除了我以上總結的這些,還有太多非常實用的方法,大家可以根據自己的需求去搜索啦!
我把這一期的ipynb檔案和py檔案放到了Github上,大家如果想要下載可以點選下面的連結:
- Github倉庫地址:https://github.com/yaozeliang/pandas_share
希望大家能夠繼續支援我,完結,撒花