Benno_Banton
Benno_Banton

Reputation: 113

How to insert multiple consecutive columns with empty values to python dataframe

I have a dataframe stations with four columns "1990", "2000", "2006", and "2012" with area data. To interpolate the years in between I want to insert columns with empty values in the gaps.

I did use pandas.DataFrame.insert to insert columns at specific locations but couldn't find out how to do that with multiple columns like pandas.DataFrame.insert[1, ["1991":"1999"], np.nan].

Is there a way to insert multiple columns with a consecutive number/name to fill the gaps? I appreciate every help!

Upvotes: 3

Views: 111

Answers (1)

Dan
Dan

Reputation: 45752

You won't hear this often for question about pandas, but in this instance, I think looping is probably the clearest solution:

for year in range(1991, 2000):
    df[str(year)] = np.NaN. 

You can then reorder the columns afterwards.

Upvotes: 1

Related Questions