In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how pandas uses str objects. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The use of str objects
Series data type: str string
# define a Series sequence > addr = pd.Series ([... 'Washington, D.C. 20003. 'Brooklyn, NY 11211-1755. 'Omaha, NE 68154. 'Pittsburgh, PA 15211. ]) > addr.str.upper () 0 WASHINGTON, D.C. 200031 BROOKLYN, NY 11211-17552 OMAHA, NE 681543 PITTSBURGH, PA 15211dtype: object > addr.str.count (r'\ d') 051 92 53 5dtype: int64 copy Code
There are two method descriptions for the above str object:
Series.str.upper: uppercase all strings in Series
Series.str.count: counts the number of all strings in Series
In fact, it is not difficult to find that the use of this usage is very similar to string manipulation in Python. Yes, you can do the same in pandas, except that you are manipulating an entire column of string data. Still based on the above dataset, look at another operation of it:
> regex = (r'(? P [A-Za-z] +),'# one or more letters. R'(? P [Amurz] {2})'# two capital letters. R'(? P\ d {5} (?:-d {4})) # four optional extended digits. > addr.str.replace ('.',') .str.extract (regex) city state zip0 Washington DC 200031 Brooklyn NY 11211-17552 Omaha NE 681543 Pittsburgh PA 15211 copy code
There are two method descriptions for the above str object:
Series.str.replace: replaces the specified string in Series
Series.str.extract: extracting data information from a string through regular expressions
This usage is a little complicated, because it is obvious that it is a chain usage. Replace "." with "" through replace, which is empty, and then use three regular expressions (corresponding to city,state,zip) to extract the data through extract, and change the original Series data structure to DataFrame data structure.
Of course, in addition to the above usage, the common properties and methods are .rstrip, .strip, split, and so on. Let's take a look at the complete list of str properties with the following code:
> > [i for i in dir (pd.Series.str) if not i.startswith ('_')] ['capitalize',' cat', 'center',' contains', 'count',' decode', 'encode',' endswith', 'extract',' extractall', 'find',' findall', 'get',' get_dummies', 'index',' isalnum', 'isalpha',' isdecimal', 'isdigit',' islower', 'isnumeric' 'isspace', 'istitle',' isupper', 'join',' len', 'ljust',' lower', 'lstrip',' match', 'normalize',' pad', 'partition',' repeat', 'replace',' rfind', 'rindex',' rjust', 'rpartition',' rsplit', 'rstrip',' slice', 'slice_replace',' split', 'startswith',' strip', 'swapcase' 'title',' translate', 'upper',' wrap', 'zfill'] Thank you for reading! This is the end of this article on "how pandas uses str objects". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.