Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Why does Python need the DataFrame type of Pandas

Shulou Source: shulou.com Published: 2022-06-02 04:43:39 09月10日 Update

This article mainly explains why Python needs the DataFrame type of Pandas. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn why Python needs the DataFrame type of Pandas.

1. Problem description

Suppose there is a need to collect basic movie data on a movie website. The fields include movie name, movie URL connection address and movie score. Imagine what data type should be selected to store this data?

The first thing you might think of is the list () type, and you'll soon write the following code:

Videos = [] item = ['the Devil Child of Najai', 'http://www.videos.com',' 9.9'] videos.append (item)

# [['the Devil Boy of Najai', 'http://www.videos.com',' 9.9]]

Although you can store this data, it will be very troublesome to find and modify some operations based on this data, such as sorting by score, super-finding records containing specific keywords, and so on. The same problem is faced with numpy.ndarray ().

The form of data described above is a common kind of data that needs to be stored and processed, but neither list () nor numpy.ndarray () can handle the data well, so we need a new and more convenient data type, which is the DataFrame type provided to us by pandas.

2. DataFrame type of Pandas

Pandas is a commonly used third-party library in Python development, and DataFrame is the most commonly used data type, which is a container for storing data. The common ones to store data in python are list () and the more powerful numpy.ndarray () in numpy, but why use DataFrame?

First, write the code to collect the basic data of the movie:

Df = pandas.DataFrame (columns= ['video_name',' video_url', 'video_score']) for i in range (1112): url =' https://www.meiju.net/search.php?page={}&searchtype=5&tid=2'.format(i) soup = BeautifulSoup (requests.get (url). Content 'html.parser') for item in soup.select (' div.hy-video-list li > a'): row = edict () row.video_name = item ['title'] row.video_url = item [' href'] score_tag = item.select_one ('span.score') if score_tag is not None: row.video_score = score _ tag.text df = df.append (row Ignore_index=True)

With the DataFrame type, what can we do?

You can quickly sort the data by score:

Df.sort_values ('video_score', ascending=False)

In addition to the above, there are many more powerful features and methods, and subsequent articles will continue to cover them based on a number of cases.

3. Conclusion

This paper introduces how to use the DataFrame type of Pandas to store the data of movie data set, and introduces the very convenient data operation provided by DataFrame. At this point, I believe you have a deeper understanding of "why Python needs the DataFrame type of Pandas". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Data type movie storage scoring power code content function field common common method problem problem processing learning sorting practical deeper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Information OPPO Reno Shulou Tech Info Linux