Ten Python tips cover 90% of data analysis needs!
The daily work of data analysts involves various tasks, such as data preprocessing, data analysis, machine learning model creation, and model deployment.
In this article, I will share 10 Python operations that can cover 90% of data analysis problems. Gain some likes, favorites, and attention.
1. Reading data sets
Reading data is an integral part of data analysis. Understanding how to read data from different file formats is the first step for a data analyst. Here is an example of how to use pandas to read a csv file containing Covid-19 data.
import pandas as pd # reading the countries_data file along with the location within read_csv function. countries_df = pd.read_csv('C:/Users/anmol/Desktop/Courses/Python for Data Science/Code/countries_data.csv') # showing the first 5 rows of the dataframe countries_df.head()
The following is the output of countries_df.head(), we can use it to view the first 5 rows of the data frame:
2. Summary statistics
The next step is to understand the data by looking at the data summary, such as the count of numeric columns such as NewConfirmed and TotalConfirmed, the mean, standard deviation, quantile, and the frequency and highest occurrence value of categorical columns such as country code
<span style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">countries_df</span>.<span style="color: rgb(0, 92, 197); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">describe</span>()
Using the describe function, we can get a summary of the continuous variables of the data set, as follows:
In the describe() function, we can set the parameter "include = ' all'" to obtain the summary of continuous variables and categorical variables
countries_df.describe(include = 'all')
3. Data selection and filtering
Analysis does not actually require all rows and sums of the data set Columns, just select the columns of interest and filter some rows based on the question.
For example, we can use the following code to select the Country and NewConfirmed columns:
countries_df[['Country','NewConfirmed']]
We can also filter the data Country, using loc, we can filter the column based on some values, as shown below:
countries_df.loc[countries_df['Country'] == 'United States of America']
4. Aggregation
Data aggregation such as count, sum, mean, etc. is one of the most commonly performed tasks in data analysis.
We can use aggregation to find the total number of NewConfimed cases by country. Use the groupby and agg functions to perform aggregation.
countries_df.groupby(['Country']).agg({'NewConfirmed':'sum'})
5, Join
Use the Join operation to combine 2 data sets into one data set.
For example: One dataset may contain the number of Covid-19 cases in different countries, and another dataset may contain latitude and longitude information for different countries.
Now we need to combine these two information, then we can perform the connection operation as shown below
countries_lat_lon = pd.read_excel('C:/Users/anmol/Desktop/Courses/Python for Data Science/Code/countries_lat_lon.xlsx') # joining the 2 dataframe : countries_df and countries_lat_lon # syntax : pd.merge(left_df, right_df, on = 'on_column', how = 'type_of_join') joined_df = pd.merge(countries_df, countries_lat_lon, on = 'CountryCode', how = 'inner') joined_df
6. Built-in functions
Understand the mathematical built-in functions, such as min(), max(), mean(), sum(), etc., are very helpful in performing different analyses.
We can apply these functions directly on the data frame by calling them. These functions can be used independently on columns or in aggregate functions, as shown below:
# finding sum of NewConfirmed cases of all the countries countries_df['NewConfirmed'].sum() # Output : 6,631,899 # finding the sum of NewConfirmed cases across different countries countries_df.groupby(['Country']).agg({'NewConfirmed':'sum'}) # Output #NewConfirmed #Country #Afghanistan75 #Albania 168 #Algeria 247 #Andorra0 #Angola53
7. User-defined Function
The functions we write ourselves are user-defined functions. We can execute the code in these functions by calling the function when needed. For example, we can create a function to add 2 numbers as follows:
# User defined function is created using 'def' keyword, followed by function definition - 'addition()' # and 2 arguments num1 and num2 def addition(num1, num2): return num1+num2 # calling the function using function name and providing the arguments print(addition(1,2)) #output : 3
8, Pivot
Pivot is to convert the unique values within a column row into multiple new columns, which is Great data processing techniques.
Using the pivot_table() function on the Covid-19 dataset, we can convert the country names into separate new columns:
# using pivot_table to convert values within the Country column into individual columns and # filling the values corresponding to these columns with numeric variable - NewConfimed pivot_df = pd.pivot_table(countries_df,columns = 'Country', values = 'NewConfirmed') pivot_df
9. Iterate over the data frame
Many When we need to traverse the indexes and rows of the data frame, we can use the iterrows function to traverse the data frame:
# iterating over the index and row of a dataframe using iterrows() function for index, row in countries_df.iterrows(): print('Index is ' + str(index)) print('Country is '+ str(row['Country'])) # Output : # Index is 0 # Country is Afghanistan # Index is 1 # Country is Albania # .......
10. String operations
Many times we process string columns in the data set, here In this case, it's important to understand some basic string operations.
For example, how to convert a string to uppercase, lowercase and how to find the length of the string.
# country column to upper case countries_df['Country_upper'] = countries_df['Country'].str.upper() # country column to lower case countries_df['CountryCode_lower']=countries_df['CountryCode'].str.lower() # finding length of characters in the country column countries_df['len'] = countries_df['Country'].str.len() countries_df.head()
The above is the detailed content of Ten Python tips cover 90% of data analysis needs!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

VS Code not only can run Python, but also provides powerful functions, including: automatically identifying Python files after installing Python extensions, providing functions such as code completion, syntax highlighting, and debugging. Relying on the installed Python environment, extensions act as bridge connection editing and Python environment. The debugging functions include setting breakpoints, step-by-step debugging, viewing variable values, and improving debugging efficiency. The integrated terminal supports running complex commands such as unit testing and package management. Supports extended configuration and enhances features such as code formatting, analysis and version control.

Yes, VS Code can run Python code. To run Python efficiently in VS Code, complete the following steps: Install the Python interpreter and configure environment variables. Install the Python extension in VS Code. Run Python code in VS Code's terminal via the command line. Use VS Code's debugging capabilities and code formatting to improve development efficiency. Adopt good programming habits and use performance analysis tools to optimize code performance.
