pandas為DataFrameThe way to format data to add a new column is very simple,只需要新建一個列索引,Then assign it a value.
以下總結了5A common way to add new columns.
首先,創建一個DataFrame結構數據,as an example of data.
import pandas as pd
# 創建一個DataFrame結構數據
data = {
'a': ['a0', 'a1', 'a2'],
'b': ['b0', 'b1', 'b2']}
df = pd.DataFrame(data)
print('舉例數據情況:\n', df)
Method to add a new column,如下:
一、insert()函數
語法:
DataFrame.insert(loc, column, value,allow_duplicates = False)
實例:插入c列
df.insert(loc=2, column='c', value=3) # after the last column,The inserted value is all3的c列
print('插入c列:\n', df)
二、直接賦值法
語法:df[‘新列名’]=new column value
實例:插入d列
df['d'] = [1, 2, 3] # 插入值為[1,2,3]的d列
print('插入d列:\n', df)
注:This method cannot choose where to insert the new column,默認為最後一列.If a new column has the same value,Just assign a constant to it directly;If the inserted value is different,in list format,It must be the same length as the number of rows in the existing column,As in the example originally listed3行,New columns are also required3個值.
三、reindex()函數
語法:df.reindex(columns=[原來所有的列名,新增列名],fill_value=值)
reindex()Function usage is more,This is just for the usage of adding a new column
實例:插入e列
df1 = df.reindex(columns=['a', 'b', 'c', 'd', 'e']) # 不加fill_value參數,默認值為Nan
df2 = df.reindex(columns=['a', 'b', 'c', 'd', 'e'], fill_value=1) # 加入fill_value參數,填充值為1
print('插入e列(不加fill_value參數):\n', df1)
print('插入e列(加fill_value參數):\n', df2)
注:This method needs to add both the original column name and the new column name,If there are too many column names,就比較麻煩.
四、concat()函數
原理:Use splicing,添加新的一列.The advantage is that multiple column names can be added at the same time.
concat()Function usage is more,This is just for the usage of adding a new column
實例:插入f列
df1 = pd.concat([df1, pd.DataFrame(columns=['f'])])
print('插入f列:\n', df1)
五、loc()函數
原理:利用locThe row and column index labels are implemented.
語法:df.loc[:,新列名]=值
實例:插入g列
df1.loc[:, 'g'] = 0
print('插入g列:\n', df1)
以上就是pandasAdd new columns5種常見用法.
【微信搜索【一位代碼】即可關注我】
-end-