pandas DataFram的insert函數


原文鏈接:https://blog.csdn.net/yanwucao/article/details/80211984

 

 

DataFrame.insert(loc, column, value, allow_duplicates=False)

 

Insert column into DataFrame at specified location.

 

Raises a ValueError if column is already contained in the DataFrame,unless allow_duplicates is set to True.

 

在指定的地方插入一列數據。如果dataframe中已經存在某列,將allow_duplicates置為true才可以將指定得列插入。

 

Parameters:
loc : int

 

Insertion index. Must verify 0 <= loc <= len(columns)  要插入的那一列

 

column : string, number, or hashable object

 

label of the inserted column    要插入那列的標簽

 

value : int, Series, or array-like

 

allow_duplicates : bool, optional    布爾類型,可選擇

 

 

 

舉實例如下:

 

import numpy as np
import pandas as pd

# A = np.array([[1, 1], [1, 2], [1, 3]])
# print(np.shape(A))

df = pd.DataFrame(np.random.randint(1, 10, (6, 4)), index=None, columns=list('ABCD'))
df['E'] = pd.Series([1, 1, 1, 1, 1, 1])
s = pd.Series([1, 1, 1, 1, 1, 1])
df.insert(1, 'X', s)
print(df)
運行結果:

 

   A  X  B  C  D  E
0  7  1  6  6  1  1
1  6  1  7  3  8  1
2  8  1  3  7  1  1
3  3  1  5  4  9  1
4  6  1  9  6  5  1
5  6  1  5  6  5  1
---------------------
作者:yanwucao
來源:CSDN
原文:https://blog.csdn.net/yanwucao/article/details/80211984
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 


免責聲明!

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



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