简体   繁体   English

在python中使用Pandas将列追加到数据框

[英]Appending a column to data frame using Pandas in python

I'm trying some operations on Excel file using pandas. 我正在尝试使用熊猫对Excel文件进​​行一些操作。 I want to extract some columns from a excel file and add another column to those extracted columns. 我想从excel文件中提取一些列,并将另一列添加到这些提取的列中。 And want to write all the columns to new excel file. 并希望将所有列写入新的Excel文件。 To do this I have to append new column to old columns. 为此,我必须将新列追加到旧列。

Here is my code- 这是我的代码-

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') 

No error is shown. 没有显示错误。 But Final_data is not giving me expected results. 但是Final_data没有给我预期的结果。 ( Data not getting appended) (数据未附加)

There is no need to explicitly append columns in pandas . 无需在pandas显式附加列。 When you calculate a new column, it is included in the dataframe. 计算新列时,它会包含在数据框中。 When you export it to excel, the new column will be included. 当您将其导出到excel时,将包括新列。

Try this, assuming 'Num Labels' and 'Num Tracks' are in "D,I,J,AU" [otherwise add them]: 尝试此操作,假设“数字标签”和“数字轨道”位于“ 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') 

The append function of a dataframe adds rows, not columns to the dataframe. 数据框的append功能会将行而不是列添加到数据框。 Well, it does add columns if the appended rows have more columns than in the source dataframe. 好吧,如果附加行的列比源数据帧中的列多,它的确会添加列。

DataFrame.append(other, ignore_index=False, verify_integrity=False)[source] DataFrame.append(其他,ignore_index = False,verify_integrity = False)[源代码]

Append rows of other to the end of this frame, returning a new object. 将其他附加到该帧的末尾,返回一个新对象。 Columns not in this frame are added as new columns. 不在此框架中的列将作为新列添加。

I think you are looking for something like concat . 我认为您正在寻找concat东西。

Combine DataFrame objects horizontally along the x axis by passing in axis=1. 通过传入axis = 1沿x轴水平组合DataFrame对象。

>>> df1 = pd.DataFrame([['a', 1], ['b', 2]],
...                    columns=['letter', 'number'])
>>> df4 = pd.DataFrame([['bird', 'polly'], ['monkey', 'george']],
...                    columns=['animal', 'name'])
>>> pd.concat([df1, df4], axis=1)
  letter  number  animal    name
0      a       1    bird   polly
1      b       2  monkey  george

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM