Hi, I’m Jiejie
Today I’m going to introduce a super liver product! Pandas is a numpy-based tool that was created to solve data analysis tasks. It provides a large number of functions and methods that allow us to process data quickly and easily. The 20 functions described in this article are absolutely data-processing killers that you’ll love to use.
Construct data set
I’m going to construct a data set for you to demonstrate these 20 functions.
Import pandas as pd df = {' name ': [' huang', 'yellow supreme', 'huang Lao evil', 'Chen Damei', 'sun shangxiang']. ['Huang tong_xue',' Huang ZHI_zun ','Huang Lao_xie','Chen Da_mei',' Sun Shang_xiang '], [' male ','women','men',' female ',' male '], 'ID ':['463895200003128433','429475199912122345','420934199110102311','431085200005230122','420953199509082345'], 'height' : [' _good mid - 175 ', 'low: 165 _bad', 'low: 159 _bad', 'high: 180 _verygood', 'low: 172 _bad']. 'Home Address ':[' Guangshui, Hubei ',' Xinyang, Henan ',' Guilin, Guangxi ',' Xiaogan, Hubei ',' Guangzhou, Guangdong '] 'Telephone Number ':['13434813546','19748672895','16728613064','14561586431','19384683910'], 'income: [' 11000', '8500', '09000', '6500', '20000']} df = pd DataFrame df (df)Copy the code
Effect:
1. The cat function
This function is mainly used for concatenating strings;
If (df[" home "] ='-'*3) if (df[" home "] ='-'*3)Copy the code
Effect:
2. The contains function
This function is used to determine whether a string contains a given character.
Df [" home address "].str. Contains (" wide ")</pre>Copy the code
Effect:
3. Startswith, endswith functions
This function is used to determine if a string begins with… Beginning/ending;
# "29" is the first line Spaces at the beginning of df [r]. "name" STR. Startswith (" yellow ") df [r]. "English name" STR. Endswith (" e ") < / pre >Copy the code
Effect:
4. The count function
This function counts the number of occurrences of a given character in a string.
Df [" phone number "].str.count("3")</pre>Copy the code
Effect:
5. The get function
This function is used to get a string at a specified position;
Df (" name "). The STR. Get (1) df [r]. "height" STR. The split (" : ") df [r]. "height" STR. The split (" : "). STR. Get (0) < / pre >Copy the code
Effect:
6. The len function
This function is mainly used to calculate the length of a string;
Df [r]. "gender" STR. Len () < / pre >Copy the code
Effect:
7. Upper and lower functions
This function is mainly used for English case conversion;
Df ["英文名 称 "].str.upper() df["英文名 称 "].str.lower()</pre>Copy the code
Effect:
8. Pad +side argument /center function
This function is mainly used to add a given character to the left, right, or left of a string;
Df [r]. "home address" STR. The pad (# 10, fillchar = "*") is equivalent to ljust (df) [r]. "home address" STR. Pad (10, side = "right", fillchar = "*") # equivalent rjust () Df [" home address "].str. Center (10,fillchar="*")</pre>Copy the code
Effect:
9. Repeat function
This function is mainly used to repeat the string several times;
Df [r]. "gender" STR. Repeat (3) < / pre >Copy the code
Effect:
10. Slice_replace function
This function is used to replace characters at a specified position with a given string.
Df [" phone number "].str.slice_replace(4,8,"*"*4)</pre>Copy the code
Effect:
11. The replace function
This function is used to replace a character at a specified position with a given string.
Df (" height "). The STR. Replace (" : ", "-") < / pre >Copy the code
Effect:
This function also takes a regular expression that replaces the character at the specified position with the given string.
Df [" income "].str.replace("\d+\.\d+"," re ")</pre>Copy the code
Effect:
12. Split method +expand parameter
This function is mainly used to expand a column into several columns;
# df[" height "].str.split(":") Expand (":",expand=True) df # split expand= join Df (" height "). The STR. The split (" : "). The STR. Join ("?" *5)</pre>Copy the code
Effect:
Strip, rstrip, lstrip function
This function is mainly used to remove whitespace, newline characters;
Df (" name "). The STR. Len (df) (" name ") = df [r]. "name" STR. The strip (df) [r]. "name" STR. Len () < / pre >Copy the code
Effect:
14. The.findall function
This function is mainly used to use regular expressions to remove matches in strings and return a list of search results.
Df [" height "].str.findall("[a-za-z]+")</pre>Copy the code
Effect:
15. Extract, Extractall function
This function takes the regular expression and extracts the matching string (always in parentheses);
Df (" height "). The STR. Extract (" ([a zA - Z] +) ") # extractall extraction to get composite index df [r]. "height" STR. Extractall (" ([a zA - Z] +) ") # extract collocation expand parameters Df [r]. "height" STR. Extract (" ([a zA - Z] +). *? ([a-zA-Z]+)",expand=True)</pre>Copy the code
Effect:
If you think this article is useful to you, don’t forget the triple link, because this will be my strongest motivation to continue to output more quality articles!