8

I have a CSV file for example like this :

id name email physics chemistry maths
1 Sta [email protected] 67 78 90
2 Danny [email protected] 77 98 89
3 Elle [email protected] 77 67 90

Now I want to output a new CSV file using pandas which has new columns too for example like this :

id name grade address physics chemistry attendance maths total

I want to create new columns in random places and I want to place the value as blank in the new columns.

I have tried using :

import pandas as pd

df = pd.read_csv("sample.csv")
final_df = df.loc[['id','name','grade','address','physics','chemistry','attendance','maths','total']]

When I did this I got an error :

KeyError(f“None of [{key}] are in the [{axis_name}]”)

Any ideas or suggestions to arrange this.

2 Answers 2

3

Let's assume you have your dataframe already, we use the Code you provided:

import pandas as pd

df = pd.read_csv("sample.csv")

Your extra columns should be stored in a list, this makes your Code easier to write and easier to understand.

additional_cols = ['grade','address','attendance','total']

The best way to add multiple columns is reindex like U11-Forward suggested.

mydf = df.reindex(df.columns.tolist() + additional_cols, axis = 1)

A similar question has been asked here[1]: How to add an empty column to a dataframe?

Regarding your error, the documentation helps you with that: The loc method is used to access elements and not to create them. If an element is not accessible a key error is returned.

1

If you want to add new columns you should try reindex with axis=1:

import pandas as pd

df = pd.read_csv("sample.csv")
final_df = df.reindex(['id','name','grade','address','physics','chemistry','attendance','maths','total'], axis=1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.