程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

pandas - common way to add new columns

編輯:Python

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)

參數說明loc必要字段,int類型數據,表示插入新列的列位置,原來在該位置的列將向右移.column必要字段,插入新列的列名.value必要字段,新列插入的值.如果僅提供一個值,將為所有行設置相同的值.可以是int,string,float等,甚至可以是series /值列表.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-


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved