8

I have a CSV file for example like this :

id name email physics chemistry maths
1 Sta sta@example.com 67 78 90
2 Danny dany@example.com 77 98 89
3 Elle elle@example.com 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.

imxitiz
  • 3,920
  • 3
  • 9
  • 33
Atom Store
  • 961
  • 1
  • 11
  • 35

2 Answers2

2

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)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114