7

I am trying to add an empty column after the 3ed column on my data frame that contains 5 columns. Example:

Fname,Lname,city,state,zip

mike,smith,new york,ny,11101

This is what I have and below I am going to show what I want it to look like.

Fname,Lname,new column,city,state,zip

mike,smith,,new york,ny,11101

I dont want to populate that column with data all I want to do is add the extra column in the header and that data will have the blank column aka ',,'. Ive seen examples where a new column is added to the end of a data frame but not at a specific placement.

2
  • Add the column then use reindex with axis=1 to order columns the way you want. Commented May 2, 2019 at 17:44
  • @ScottBoston what would I use to write this to a new csv file named 'new.csv' Commented May 2, 2019 at 18:09

3 Answers 3

18

you should use

df.insert(loc, column, value)

with loc being the index and column the column name and value it's value

for an empty column

df.insert(loc=2, column='new col', value=['' for i in range(df.shape[0])])

Sign up to request clarification or add additional context in comments.

10 Comments

what would I use to write this to a new csv file named 'new.csv'
df.to_csv('new.csv',index=False)
That is exactly what I have but for some reason its not putting out another file.
import pandas as pd df = pd.read_csv (r'C:\Users\kziaj\Desktop\Python Test\new.csv') df.insert(loc=1, column='avalue', value=[',' for i in range(df.shape[0])]) df.to_csv('new1.csv',index=False) #Print in new file
No im not im using visual studio code off my desktop
|
3

Use reindex or column filtering

df = pd.DataFrame(np.arange(50).reshape(10,-1), columns=[*'ABCDE'])

df['z']= np.nan

df[['A','z','B','C','D','E']]

OR

df.reindex(['A','z','B','C','D','E'], axis=1)

Output:

    A   z   B   C   D   E
0   0 NaN   1   2   3   4
1   5 NaN   6   7   8   9
2  10 NaN  11  12  13  14
3  15 NaN  16  17  18  19
4  20 NaN  21  22  23  24
5  25 NaN  26  27  28  29
6  30 NaN  31  32  33  34
7  35 NaN  36  37  38  39
8  40 NaN  41  42  43  44
9  45 NaN  46  47  48  49

3 Comments

what would I use to write this to a new csv file named 'new.csv'
to_csv('new.csv') you might want to fillna('') and/or index=False.
would you be able to script this for some reason its not creating a new file
2

You can simply go for df.insert()

import pandas as pd
data = {'Fname': ['mike'], 
        'Lname': ['smith'], 
        'city': ['new york'],
        'state': ['ny'],
        'zip': [11101]}
df = pd.DataFrame(data) 
df.insert(1, "Address", '', True)
print(df)

Output:

  Fname Address  Lname      city state    zip
0  mike          smith  new york    ny  11101

2 Comments

what would I use to write this to a new csv file named 'new.csv'
for saving the df to a new file "new.csv", use df.to_csv(filename)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.