Pandas DataFrame實現(xiàn)任意位置插入一列或一行
新建DataFrame
import numpy as np import pandas as pd arr = [11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43] # 轉化為4行3列的numpy數組 np_arr = np.array(arr).reshape((4, 3)) # 轉化為DataFrame pd_arr = pd.DataFrame(np_arr)
結果:
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 11 | 12 | 13 |
| 1 | 21 | 22 | 23 |
| 2 | 31 | 32 | 33 |
| 3 | 41 | 42 | 43 |
插入一列
任意位置插入一列比較簡單,用自帶的pd.insert即可實現(xiàn)
# 待插入列索引 col_n = 1 # 待插入數據,以一維列表為例 d = ["*"] * 4 # 參數:添加列索引,添加列名,添加數據,是否允許列名重復 pd_arr.insert(col_n, "*", d, allow_duplicates=False)
結果:
| 0 | * | 1 | 2 | |
|---|---|---|---|---|
| 0 | 11 | * | 12 | 13 |
| 1 | 21 | * | 22 | 23 |
| 2 | 31 | * | 32 | 33 |
| 3 | 41 | * | 42 | 43 |
插入一行
任意位置插入一行時自帶的pd.append無法實現(xiàn)(append只能在最后添加一行),需要先將原DataFrame拆分,添加新數據后再組合
# 待插入行索引 row_n = 1 # 待插入數據,以一維列表為例 d = [["*"] * 4] # 拆分 pd_arr1 = [:row_n] pd_arr2 = [row_n:] # 參數:添加數據,是否無視行索引 pd_arr = pd_arr1.append(d, ignore_index=True).append(pd_arr2, ignore_index=True)
結果:
| 0 | * | 1 | 2 | |
|---|---|---|---|---|
| 0 | 11 | * | 12 | 13 |
| 1 | * | * | * | * |
| 2 | 21 | * | 22 | 23 |
| 3 | 31 | * | 32 | 33 |
| 4 | 41 | * | 42 | 43 |
可以將這幾行代碼整理成一個方法,以便于重復調用,如(以添加一維列表為例):
def df_insert(df, n, arr, ignore_index=True): """ DataFrame任意位置添加一行 :param df: DataFrame :param n: 添加行號索引 :param arr: 添加數據 :param ignore_index: 是否無視行索引,為True則重新從0生成df的行號 :return: DataFrame """ # 如果原df列名被修改,則需要給新插入的行也賦予列名 # arr = pd.DataFrame(np.array(arr).reshape((1, len(arr))), columns=df.columns) # 否則直接插入二維數組即可 arr = [arr] df1 = df[:n] df2 = df[n:] df0 = df1.append(arr, ignore_index).append(df2, ignore_index) return df0 # 待插入行索引 row_n = 1 # 待插入數據,以一維列表為例 d = ["*"] * 4 # 調用方法 pd_arr = df_insert(pd_arr, row_n, d)
補充
用yield迭代DataFrame時插入新的行,迭代結果不受影響
def df_yield(df):
# index=True時返回結果Pandas類型數據中將帶Index
# 即 Pandas(Index=*, column_name=*, ...)
for row in df.itertuples(index=False):
yield row
y = df_yield(df_arr)
# 用try except StopIteration判斷迭代結束
y0 = next(y)
print("index 0 ", y0)
# index 0 插入新的數據
pd_arr = pd_insert(pd_arr, 0, ["*"] * 3)
y1 = next(y)
print("index 1 ", y1)結果:
index 0 Pandas(_0=11, _1=12, _2=13)
index 1 Pandas(_0=21, _1=22, _2=23)
沒有受到新插入數據的影響
到此這篇關于Pandas DataFrame實現(xiàn)任意位置插入一列或一行的文章就介紹到這了,更多相關Pandas DataFrame任意位置插入內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)根據IP地址和子網掩碼算出網段的方法
這篇文章主要介紹了Python實現(xiàn)根據IP地址和子網掩碼算出網段的方法,涉及Python基于Linux平臺的字符串操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
python pymysql peewee關于時區(qū)問題分析
這篇文章主要為大家介紹了python pymysql peewee關于時區(qū)問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
關于torch.scatter與torch_scatter庫的使用整理
這篇文章主要介紹了關于torch.scatter與torch_scatter庫的使用整理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
詳解在Anaconda環(huán)境下Python安裝pydot與graphviz的方法
這篇文章主要為大家詳細介紹了在Anaconda環(huán)境中,安裝Python語言pydot與graphviz兩個模塊的方法,文中的安裝方法講解詳細,感興趣?的可以了解一下2023-02-02
python庫geopy計算多組經緯度距離的實現(xiàn)方式
這篇文章主要介紹了python庫geopy計算多組經緯度距離的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Python對文件和目錄進行操作的方法(file對象/os/os.path/shutil 模塊)
下面小編就為大家?guī)硪黄狿ython對文件和目錄進行操作的方法(file對象/os/os.path/shutil 模塊)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

