您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么使用Python中Pandas的索引對齊方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
一.索引對象支持集合運算:聯合、交叉、求差、對稱差
Demo1:
import pandas as pd import numpy as np college = pd.read_csv('data/college.csv') columns = college.columns c1 = columns[:4] c2 = columns[2:5] print(c1.union(c2)) print(c1 | c2)
Demo2:
import pandas as pd import numpy as np college = pd.read_csv('data/college.csv') columns = college.columns c1 = columns[:4] c2 = columns[2:5] print("c1 : ",c1) print("c2 : ",c2) print(c1.symmetric_difference(c2)) print(c1 ^ c2)
二.用copy()產生新的數據
A is B:表明二者指向的同一個對象。這意味著,如果修改一個,另一個也會去改變。
Demo1:
import pandas as pd import numpy as np employee = pd.read_csv('data/employee.csv', index_col='RACE') salary1 = employee['BASE_SALARY'] salary2 = employee['BASE_SALARY'] print(salary1 is salary2) salary1 = employee['BASE_SALARY'].copy() salary2 = employee['BASE_SALARY'].copy() print(salary1 is salary2)
三.不等索引(索引的difference方法)
Demo1:
用difference,找到哪些索引標簽在baseball_14中,卻不在baseball_15、baseball_16中
import pandas as pd import numpy as np baseball_14 = pd.read_csv('data/baseball14.csv', index_col='playerID') baseball_15 = pd.read_csv('data/baseball15.csv', index_col='playerID') baseball_16 = pd.read_csv('data/baseball16.csv', index_col='playerID') print(baseball_14.index.difference(baseball_15.index)) print(baseball_14.index.difference(baseball_16.index))
四.使用fill_value避免在算術運算時產生缺失值
Demo1:
import pandas as pd import numpy as np baseball_14 = pd.read_csv('data/baseball14.csv', index_col='playerID') baseball_15 = pd.read_csv('data/baseball15.csv', index_col='playerID') #H列:每名球員的擊球數 hits_14 = baseball_14['H'] hits_15 = baseball_15['H'] print(hits_14.head()) print(hits_15.head()) print(hits_14.head() + hits_15.head())
下面四條數據是有記錄的,但是因為不同時存在14,15兩張表中,所以相加會產生NaN,需要用fill_value
Demo2:
import pandas as pd import numpy as np baseball_14 = pd.read_csv('data/baseball14.csv', index_col='playerID') baseball_15 = pd.read_csv('data/baseball15.csv', index_col='playerID') baseball_16 = pd.read_csv('data/baseball16.csv', index_col='playerID') #H列:每名球員的擊球數 hits_14 = baseball_14['H'] hits_15 = baseball_15['H'] hits_16 = baseball_16['H'] print(hits_14.head().add(hits_15.head(),fill_value=0))
*如果一個元素在兩個Series都是缺失值,即便使用了fill_value,相加的結果也仍是缺失值
五.從不同的DataFrame追加列
Demo:
import pandas as pd import numpy as np employee = pd.read_csv('data/employee.csv') d1 = employee[['DEPARTMENT', 'BASE_SALARY']] print("排序前:") print(d1.head()) # 在每個部門內,對BASE_SALARY進行排序 d2 = d1.sort_values(['DEPARTMENT', 'BASE_SALARY'],ascending = [True,False]) print("排序后:") print(d2.head()) #用drop_duplicates方法保留每個部門的第一行 d3 = d2.drop_duplicates(subset = 'DEPARTMENT') print('去重后:') print(d3.head()) #使用DEPARTMENT作為行索引 d3 = d3.set_index('DEPARTMENT') employee = employee.set_index('DEPARTMENT') #向employee的DataFrame新增一列 #新增時,對應缺項的為缺失值 #存儲每個Department的最高工資 employee['MAX_SALARY'] = d3['BASE_SALARY'] pd.options.display.max_columns = 3 print('合并后:') print(employee.head()) #用query查看是否有BASE_SALARY大于MAX_DEPT_SALARY的 #輸出應該為0 print('query結果:') print(employee.query('BASE_SALARY > MAX_SALARY'))
employee[‘MAX_SALARY’] = d3[‘BASE_SALARY’]
這行語句能執行成功的條件是:d3中不含有重復索引,即執行過drop_duplicates
運行結果:
“怎么使用Python中Pandas的索引對齊方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。