Home > Article > Backend Development > Summary of commonly used functions in Python's pandas
This article brings you a summary of commonly used functions in Python's pandas. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
pandas is a data processing library in python. When using it, we must first enter import pandas as pd
to introduce.
1.df = pd.read_csv("File Path"): This is the method to read csv files. If you want to read excel or other documents, there is a corresponding read function.
2.df.dtypes: If there is character data in the file, object is returned.
3.df.head(n): Display the first n rows of data. If no parameters are passed in, the first 5 rows of data will be displayed.
4.df.tail(n): Display the last n rows of data. If no parameters are passed in, the last 5 rows of data will be displayed.
5.df.columns: Display the column names of the data table in the form of a list.
6.df.shape: Display the number of rows and columns of data in the table in the form of tuples.
7.df.loc[n]: Returns the row with index value n.
8.df.loc[m][n]: Returns the data whose index value is m rows and n columns.
9.df.loc[m:n]: Returns rows with index values from m to n.
10.df.loc[[m,n,k]]: Returns the rows whose index values are m,n,k respectively.
11.df["str"]: Returns the column named str.
12.df.columns.tolist(): Make column names into a list.
13.df["str"]*df["str"]: The dimensions of the two columns are the same, then the corresponding positions of the two columns are multiplied. .
14.df.sort_values("str",inplace=True,ascending=False): Arrange the str column in descending order, and get The data replaces the original data. inplace indicates whether to replace the original data with the sorted data. The default is False, which means no replacement. ascending indicates the sorting order, the default is True, that is, arranged in ascending order.
15.judge = pd.isnull(df["str"]): Returns a bool value. The data in the str column is a null value. Returns True, otherwise returns False.
16.a["judge"]: Return judge as True, which is the missing data. At this time, call the len() function. The number of missing data can be found.
##17.df.pivot_table(index="a",values= "b",aggfunc=np.mean): This is a very important function. It averages b and classifies it according to the category of a. The third parameter defaults to average.
18.df.loc[n,"str"]: Locate the data at row n, column name str.
19.sort_res.reset_index(drop=True): Rearrange the numbers of the sorted data. Drop refers to whether to discard the original data. Comparing the running results with Figure 14, we can see that the numbers have been rearranged.
20.df.apply(): This is how to use a custom function in pandas. The function name is passed in parentheses.
The above is the detailed content of Summary of commonly used functions in Python's pandas. For more information, please follow other related articles on the PHP Chinese website!