pandas的concat和drop函數


pandas的concat函數用於連接兩個DataFrame數據,drop函數用於刪除DataFrame數據中的行或者列。

首先導入需要的庫 pandas和numpy:

import pandas as pd
import numpy as np

1. 創建DataFrame,可以使用數組或者列表、字典來創建數據

df_1 = pd.DataFrame([['Jack','M',40],['Tony','M',20],['Mary','F',30],['Bob','M',25]], columns=['name','gender','age'])  #列表創建DataFrame
print('------ df_1是: ------')
print(df_1)

df_2 = pd.DataFrame(np.arange(16).reshape((4, 4)), index = ['aa', 'bb', 'cc', 'dd'], columns = ['c1', 'c2', 'c3', 'c4'])  #數組創建DataFrame
print('------ df_2是: ------')
print(df_2)

array_test = np.array([[1,2,3,4],[2,3,4,5],[3,4,5,6]])
df_3 = pd.DataFrame(array_test, index = ['aa', 'bb', 'ff'], columns = ['c1', 'c2', 'c3', 'c4'])  #數組創建DataFrame
print('------ df_3是: ------')
print(df_3)

dict_test = {'c1':[1,2,3,4],'c2':[2,3,4,5],'c3':[3,4,5,6]}
df_20 = pd.DataFrame(dict_test)  #字典創建DataFrame
print('------ df_20是: ------')
print(df_20)

2. 也可創建一個序列Series

s_1 = pd.Series(np.arange(4),index= ['aa','bb','cc','dd'])
print('------ s_1是: ------')
print(s_1)

3. concat :連接多個DataFrame,axis=0 表示按行連接,即在行的方向上進行擴充連接axis=1表示按列連接,即在列的方向上進行擴充連接;ignore_index表示連接數據忽略之前的索引,統一使用新的索引;join默認值是'outer';axis=0時,下面的結果中沒有NaN值表示兩者的列名一致

df_4 = pd.concat([df_2, df_3], axis=0, ignore_index=True)
print('------ df_4是: ------')
print(df_4)

4. concat:axis=1表示列連接,即在列的維度上進行連接,join默認值是'outer',表示默認外連接,類似於sql;按列連接時相同行名稱的直接連接不同行名稱時用NaN填充

df_5 = pd.concat([df_2, df_3], axis=1, ignore_index=True, sort=False)
print('------ df_5是: ------')
print(df_5)

5. concat:ignore_index=False均表示保留之前的索引

df_6 = pd.concat([df_2, df_3], axis=1, ignore_index=False, sort=False)
print('------ df_6是: ------')
print(df_6)

6. concat:join='inner'表示內連接,只會保留兩者均有的索引行/列

df_7 = pd.concat([df_2, df_3], axis=1, join='inner', ignore_index=False, sort=False)
print('------ df_7是: ------')
print(df_7)

7. drop:刪除其中某幾行/列,labels表示要刪掉的行索引或者列名,必須是已存在的;axis=0表示按行

df_8 = df_2.drop(labels=['bb'], axis=0)  #直接drop時,索引會有缺失,特別是當數字作為索引時,刪除中間的索引行容易造成讀取錯誤
print('------ df_8是: ------')
print(df_8)

注:直接drop時,索引會有缺失,特別是當數字作為索引時,刪除中間的索引行容易造成讀取錯誤

8. drop:刪除行時,reset_index會重建新索引

df_9 = df_2.drop(labels=['bb'], axis=0).reset_index(drop=True)
print('------ df_9是: ------')
print(df_9)

9. drop:刪除列,類似;axis=1表示按列

df_10 = df_2.drop(labels=['c3'], axis=1)
print('------ df_10是: ------')
print(df_10)

10. drop:method='bfill' 表示用后面的值向前面填充(否則會用NaN值填充),reindex用於重建索引

df_11 = df_2.drop(labels=['c3'], axis=1).reindex(columns=['c1','c2','c3','c4'], method='bfill')
print('------ df_11是: ------')
print(df_11)

11. drop:method='ffill'表示用前面的值向后面填充

df_12 = df_2.drop(labels=['c3'], axis=1).reindex(columns=['c1','c2','c3','c4'], method='ffill')
print('------ df_12是: ------')
print(df_12)

12. drop:fill_value=0表示用0值填充

df_13 = df_2.drop(labels=['c3'], axis=1).reindex(columns=['c1','c2','c3','c4'], fill_value=0)
print('------ df_13是: ------')
print(df_13)

 

## 

參考:

https://jingyan.baidu.com/article/91f5db1b79205a1c7f05e3ae.html

https://blog.csdn.net/HHG20171226/article/details/101391045

https://www.jianshu.com/p/67452926fa2d


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM