原文链接: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
版权声明:本文为博主原创文章,转载请附上博文链接!