TC1111
TC1111

Reputation: 89

How to add a column from one file to another using pandas

I have two data sets: grades.csv and rubric.csv

A sample from the grades csv is below:

Student ID,Question 1,Question 2,Question 3,Question 4,Question 5,Question 6
205842,6.5,6.5,9.5,5.5,3.5,9.5
280642,8.5,9.5,3.5,9.5,4,9.5

and the rubric csv looks like this:

Question,Max score
Question 1, 20
Question 2, 10
Question 3, 10
Question 4, 15
Question 5, 10
Question 6, 25

I want to be able to add the 'Max Score' column from the rubric csv as another column in the grades csv.

So far I have the below. I am assuming the grades.csv needs to be deconstructed or inverted for t

grades_df = pd.read_csv(grades)
rubric_df = pd.read_csv(rubric)
grades_dft = grades_df.T

Upvotes: 1

Views: 213

Answers (1)

marcos
marcos

Reputation: 4510

Just assign it like this:

grades_df = pd.read_csv(grades)
rubric_df = pd.read_csv(rubric)
grades_df['Max score'] = rubric_df['Max score']
print(grades_df)

Or if you want to be very explicit adding a new column like @jakub mention:

grades_df.loc[:, 'Max_score'] = rubric_df['Max score']

You will get this:

   Student ID  Question 1  Question 2  Question 3  Question 4  Question 5  Question 6  Max score
0      205842         6.5         6.5         9.5         5.5         3.5         9.5         20
1      280642         8.5         9.5         3.5         9.5         4.0         9.5         10

Upvotes: 1

Related Questions