search
HomeBackend DevelopmentPython TutorialPython For Data Analysis learning path

Python For Data Analysis learning path

Jun 23, 2017 pm 04:25 PM
analysisdataforpythonstudynotes

In the introductory chapter, an example of processing the MovieLens 1M data set is introduced. The book introduces that the data set comes from GroupLens Research (), this address will jump directly to it, which provides various evaluation data sets from the MovieLens website, and you can download the corresponding compressed package. The MovieLens 1M data set we need is also there. in.

The downloaded and decompressed folder is as follows:

These three dat tables will be used in the example. The Chinese version (PDF) of "Python For Data Analysis" I read is the first edition in 2014. All the examples in it are written based on Python 2.7 and pandas 0.8.2, and I installed Python 3.5.2 and pandas 0.8.2. pandas 0.20.2, some functions and methods in it will be quite different. Some of them are parameters changed in the new version, while some are deprecated in the new version. This caused me to run according to the book When sample code, you will encounter some Errors and Warnings. When testing the MovieLens 1M data set code, under the same configuration environment as mine, I will encounter the following problems.

  • When reading dat data into a pandas DataFrame object, the code given in the book is:

    users = pd.read_table('ml-1m/users.dat', sep='::', header=None, names=unames)
    
    rnames = ['user_id', 'movie_id', 'rating', 'timestamp']
    ratings = pd.read_table('ml-1m/ratings.dat', sep='::', header=None, names=rnames)
    
    mnames = ['movie_id', 'title', 'genres']
    movies = pd.read_table('ml-1m/movies.dat', sep='::', header=None, names=mnames)

    When running directly, a Warning will appear:

    F:/python/HelloWorld/DataAnalysisByPython-1.py:4: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
      users = pd.read_table('ml-1m/users.dat', sep='::', header=None, names=unames)
    F:/python/HelloWorld/DataAnalysisByPython-1.py:7: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
      ratings = pd.read_table('ml-1m/ratings.dat', sep='::', header=None, names=rnames)
    F:/python/HelloWorld/DataAnalysisByPython-1.py:10: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
      movies = pd.read_table('ml-1m/movies.dat', sep='::', header=None, names=mnames)

    Although it can also be run, as a perfect obsessive-compulsive disorder, I still want to solve this Warning . This warning means that because the 'C' engine does not support it, it can only fall back to the 'Python' engine, and there happens to be an engine parameter in the pandas.read_table method, which is used to set which parsing engine to use, including 'C' and 'Python' These two options. Since the 'C' engine does not support it, we only need to set the engine to 'Python'.

    users = pd.read_table('ml-1m/users.dat', sep='::', header=None, names=unames, engine = 'python')
    
    rnames = ['user_id', 'movie_id', 'rating', 'timestamp']
    ratings = pd.read_table('ml-1m/ratings.dat', sep='::', header=None, names=rnames, engine = 'python')
    
    mnames = ['movie_id', 'title', 'genres']
    movies = pd.read_table('ml-1m/movies.dat', sep='::', header=None, names=mnames, engine = 'python')

  • Use the pivot_table method to calculate the average score of each movie by gender on the aggregated data. The code given in the book is:

    mean_ratings = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')

    If you run it directly, an error will be reported and this code cannot be run:

    Traceback (most recent call last):
      File "F:/python/HelloWorld/DataAnalysisByPython-1.py", line 19, in <module>mean_ratings = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')
    TypeError: pivot_table() got an unexpected keyword argument 'rows'</module>

    TypeError indicates that the 'rows' parameter here is not a keyword parameter available in the method. What is going on? I checked the pandas API usage documentation () on the official website and found that the keyword parameters in pandas.pivot_table have changed in version 0.20.2. In order to achieve the same effect, just replace rows with index. That's it, and there is no cols parameter, so use columns instead.

    mean_ratings = data.pivot_table('rating', index='title', columns='gender', aggfunc='mean')

  • #In order to understand the favorite movies of female audiences, use the DataFrame method to sort column F in descending order , the sample code in the book is:

    top_female_ratings = mean_ratings.sort_index(by='F', ascending=False)

    This only gives a Warning and will not interfere with the program:

    F:/python/HelloWorld/DataAnalysisByPython-1.py:32: FutureWarning: by argument to sort_index is deprecated, pls use .sort_values(by=...)
      top_female_ratings = mean_ratings.sort_index(by='F', ascending=False)

    This means that the sort_index method for sorting may change in the language or library in the future, and it is recommended to use sort_values ​​instead. In the API usage documentation, the description of pandas.DataFrame.sort_index is "Sort object by labels (along an axis)", while the description of pandas.DataFrame.sort_values ​​is "Sort by the values ​​along either axis". Both can To achieve the same effect, then I will just replace it with sort_values. Sort_index will also be used in the following "Calculate score difference", and can also be replaced by sort_values.

    top_female_ratings = mean_ratings.sort_values(by='F', ascending=False)

  • The last error is still related to sorting. After calculating the standard deviation of the score data in "Calculate Rating Difference", sort the Series in descending order according to the filtered value. The code in the book is:

    print(rating_std_by_title.order(ascending=False)[:10])

    这里的错误是:

    Traceback (most recent call last):
      File "F:/python/HelloWorld/DataAnalysisByPython-1.py", line 47, in <module>print(rating_std_by_title.order(ascending=False)[:10])
      File "E:\Program Files\Python35\lib\site-packages\pandas\core\generic.py", line 2970, in __getattr__return object.__getattribute__(self, name)
    AttributeError: 'Series' object has no attribute 'order'</module>

    居然已经没有这个order的方法了,只好去API文档中找替代的方法用。有两个,sort_index和sort_values,这和DataFrame中的方法一样,为了保险起见,我选择使用sort_values:

    print(rating_std_by_title.sort_values(ascending=False)[:10]

    得到的结果和数据展示的结果一样,可以放心使用。

第三方库不同版本间的差异还是挺明显的,建议是使用最新的版本,在使用时配合官网网站上的API使用文档,轻松解决各类问题~

The above is the detailed content of Python For Data Analysis learning path. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor