Home  >  Article  >  Backend Development  >  Summary of commonly used functions in Python's pandas

Summary of commonly used functions in Python's pandas

不言
不言forward
2019-01-14 11:45:1214047browse

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.

Summary of commonly used functions in Pythons pandas

2.df.dtypes: If there is character data in the file, object is returned.

Summary of commonly used functions in Pythons pandas

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.

Summary of commonly used functions in Pythons pandas

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.

Summary of commonly used functions in Pythons pandas

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.

Summary of commonly used functions in Pythons pandas

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.

Summary of commonly used functions in Pythons pandas

11.df["str"]: Returns the column named str.

Summary of commonly used functions in Pythons pandas

12.df.columns.tolist(): Make column names into a list.

Summary of commonly used functions in Pythons pandas

13.df["str"]*df["str"]: The dimensions of the two columns are the same, then the corresponding positions of the two columns are multiplied. .

Summary of commonly used functions in Pythons pandas

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.

Summary of commonly used functions in Pythons pandas

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.

Summary of commonly used functions in Pythons pandas

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.

Summary of commonly used functions in Pythons pandas

Summary of commonly used functions in Pythons pandas

##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.

Summary of commonly used functions in Pythons pandas

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.

Summary of commonly used functions in Pythons pandas

20.df.apply(): This is how to use a custom function in pandas. The function name is passed in parentheses.

Summary of commonly used functions in Pythons pandas

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete