在python中使用Pandas将列追加到数据帧
我正在使用pandas对Excel文件进行一些操作。我想从excel文件中提取一些列,并向这些提取的列添加另一列。并希望将所有列写入新的excel文件。为此,我必须将新列附加到旧列 这是我的密码-在python中使用Pandas将列追加到数据帧,python,pandas,Python,Pandas,我正在使用pandas对Excel文件进行一些操作。我想从excel文件中提取一些列,并向这些提取的列添加另一列。并希望将所有列写入新的excel文件。为此,我必须将新列附加到旧列 这是我的密码- import pandas as pd #Reading ExcelFIle #Work.xlsx is input file ex_file = 'Work.xlsx' data = pd.read_excel(ex_file,'Data') #Create subset of column
import pandas as pd
#Reading ExcelFIle
#Work.xlsx is input file
ex_file = 'Work.xlsx'
data = pd.read_excel(ex_file,'Data')
#Create subset of columns by extracting columns D,I,J,AU from the file
data_subset_columns = pd.read_excel(ex_file, 'Data', parse_cols="D,I,J,AU")
#Compute new column 'Percentage'
#'Num Labels' and 'Num Tracks' are two different columns in given file
data['Percentage'] = data['Num Labels'] / data['Num Tracks']
data1 = data['Percentage']
print data1
#Here I'm trying to append data['Percentage'] to data_subset_columns
Final_data = data_subset_columns.append(data1)
print Final_data
Final_data.to_excel('111.xlsx')
没有显示错误。但最终的数据并没有给我预期的结果。(未添加数据)无需在
pandas
中显式添加列。计算新列时,它将包含在数据帧中。将其导出到excel时,将包括新列
试试这个,假设“Num标签”和“Num轨迹”在“D,I,J,AU”中[否则添加它们]:
import pandas as pd
data_subset = pd.read_excel(ex_file, 'Data', parse_cols="D,I,J,AU")
data_subset['Percentage'] = data_subset['Num Labels'] / data_subset['Num Tracks']
data_subset.to_excel('111.xlsx')
无需在
pandas
中显式追加列。计算新列时,它将包含在数据帧中。将其导出到excel时,将包括新列
试试这个,假设“Num标签”和“Num轨迹”在“D,I,J,AU”中[否则添加它们]:
import pandas as pd
data_subset = pd.read_excel(ex_file, 'Data', parse_cols="D,I,J,AU")
data_subset['Percentage'] = data_subset['Num Labels'] / data_subset['Num Tracks']
data_subset.to_excel('111.xlsx')
数据帧的
append
函数向数据帧添加行,而不是列。如果追加的行的列数比源数据框中的列数多,则会添加列
附加(其他,忽略索引=False,验证完整性=False)[source]
将other的行追加到此帧的末尾,返回一个新对象。不在此框架中的列将作为新列添加
我想您正在寻找类似于concat
的产品
通过传入轴=1,沿x轴水平组合数据帧对象
数据帧的
append
函数向数据帧添加行,而不是列。如果追加的行的列数比源数据框中的列数多,则会添加列
附加(其他,忽略索引=False,验证完整性=False)[source]
将other的行追加到此帧的末尾,返回一个新对象。不在此框架中的列将作为新列添加
我想您正在寻找类似于concat
的产品
通过传入轴=1,沿x轴水平组合数据帧对象