Home > Enterprise >  Append extracted column to list without index: Pandas
Append extracted column to list without index: Pandas

Time:12-05

I have the dataframe as follows:

Name Year Class
Roy 2001 12
Pete 2001 12

I am executing some function where I need one segment of code to get class of particular student and append to list. I used the following.

EmpList = []
c1 = d1["Class"].loc[Emp["Name"]=="Roy"]
EmpList.append(c1)

Before appending class column, I have already succesfully appended marks of student from another dataframe. But when I append the class column and print the list, the index of the Class column also gets printed. After converting list to Dataframe I get As follows:

Name marks1 marks2 Class
Roy 23 24 0 12

and so on... The problem is only with class column. Kindly suggest Not only, index. But it is also storing Name: Class, dtype:object in the list. Shall I convert series to any other datatype? Or is there any other way to append it?

CodePudding user response:

If you are sure to have only one instance of 'Roy' in your dataframe, you can use squeeze:

EmpList = []
c1 = d1["Class"].loc[Emp["Name"]=="Roy"].squeeze()  # <- HERE
EmpList.append(c1)
  • Related