Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to Pandas Code in Python to help data Practitioners start a New Journey

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how the Pandas code in Python helps data practitioners start a new journey. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Abstract

Pandas is a tool based on NumPy, which is created to solve data analysis tasks. Many friends who use Python to do data analysis should be familiar with Pandas. Here are 20 commonly used Pandas codes to help you understand the data more quickly.

Here I divide these 20 Pandas codes into three categories:

Basic data information

Basic data processing

Manipulate Data frames

Basic data information

1. Basic read and write datasets (CSV, Execl)

# csv# read pd.DataFrame.from_csv ("csv_file") pd.read_csv ("csv_file") # write df.to_csv ("data.csv", sep= ",", index=False) # comma separated, no subscript # execlpd.read_excel ("excel_file") df.to_execl ("data.xlsx", sheet_name='a')

2. Basic data set characteristics

Df.info ()

3. Basic data statistics

Df.describe ()

4. Output data frames to a table (tabulate module)

From tabulate import tabulateprint (tabulate (print_table, headers=headers)) # print_table is the list containing the list # headers is the field contained in the header

5. List all the fields

Df.columns

6. Get before and after n lines

Df.head (n) # the first n lines df.tail (n) # the last n lines

7. Locate the data through features and locations

Df.Lok [feature _ name] # Select the first row of the "size" column, df.loc ([0], ['size']) df.iloc [n] # location

Basic data processing

8. Remove missing values

Df.dropna (axis=0, how='any')

9. Replace the missing value

Df.replace (to_replace=None, value=None) # replace the value in "to_replace" with "value"

10. Check for missing values

Pd.isnull (object) # detects missing values (NaN in numeric array, None/NaN in object array)

11. Delete a field

The df.drop ('feature_variable_name', axis=1) # axis is 0 for rows and 1 for columns

12. Convert object types to numeric values

Pd.to_numeric (df ["feature_name"], errors='coerce') # converts object types to numeric so that calculations can be performed (if they are strings)

13. Convert Dataframe to numpy array

Df.as_matrix ()

Manipulate Data frames

14. Apply the function to dataframe

# this will multiply all values in the "height" column of the data by 21, df ["height"] .apply (lambda height: 2 * height) 2, def multiply (x): return x * 2df ["height"] .apply (multiply)

15. From a named column

# here, the third column of the data will be renamed to "size" df.rename (columns = {df.columns [2]: 'size'}, inplace=True)

16. Get the unique item of a column

# here you will get the unique entry df ["name"] .unique () for column "name"

17. Multi-level access

# here, you will get the column selection from the data, "name" and "size" new_df = df [["name", "size"]]

18. Some statistics of the data df.sum ()

Df.min () df.max () df.idxmin () df.idxmax () # returns the maximum index df.mean () df.median () df.corr () # correlation coefficient between different columns df ["size"] .median

19. Data sorting

Df.sort_values (ascending = False)

20. Boolean index

Df [df ["size"] = = 5] # Boolean index above is how the Pandas code in Python is shared by the editor to help data practitioners start a new journey. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report