程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] fans pandas questions: one question and three solutions

編輯:Python

official account : Youer cottage
author :Peter
edit :Peter

Today, I would like to share a question about Pandas topic , Provide a variety of solutions !

demand ( Basic version )

There is such a DataFrame( The simulated data is ), Find the index number of the first occurrence of each value :

  • 2 The first occurrence of index is 0

  • 3 The first occurrence of index is 4

  • 7 The first occurrence of index is 6

This is the final result to show , How to solve ? Here are different solutions

Method 1- Shift function shift

shift The function moves the specified length in the specified axis direction :

1、 First move down one unit , obtain df2

2、 For later processing ,df2 Rename the attribute field :

df2.rename(columns={"col1":"col2"},inplace=True)

3、 take df1 and df2 A merger

4、 Screening col1 and col2 Different situations can be

Method 2- The difference function diff

The complete line of code is as follows :

Let's disassemble this line of code :

1、 The difference function diff

Each data is subtracted from the previous value ; If two values are equal , The difference is 0

2、 Judgement and 0 The relationship between

3、 Lock to True The value of the can

Method 3: Direct weight removal

A very clever way : Direct weight removal , Keep the first piece of data ; But this is only a special case

demand ( Upgraded version )

The requirements are similar , Find the index of the first occurrence of each value ; But the data will appear repeatedly .

The correct results have been marked below :

Method 1-shift function

It can still be solved

result :

Method 2- The difference function diff

It can also solve :

But the de duplication function doesn't work in this case :drop_duplicates A function is a de duplication of the overall data , Keep only one piece of data

Extend knowledge

Here we will focus on understanding the shift function shift Sum difference function diff The relationship between , Simulate a piece of data :

diff function

shift function

The relationship between them

Take the example of moving one unit by default

Conclusion : Under the same conditions , Original data frame Subtract shift The data generated by the function is diff The result of the function

Thinking questions

Under what circumstances will the shift or difference function be used ?

 Past highlights
It is suitable for beginners to download the route and materials of artificial intelligence ( Image & Text + video ) Introduction to machine learning series download Chinese University Courses 《 machine learning 》( Huang haiguang keynote speaker ) Print materials such as machine learning and in-depth learning notes 《 Statistical learning method 》 Code reproduction album machine learning communication qq Group 955171419, Please scan the code to join wechat group 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved