shrimpers
shrimpers

Reputation: 25

Appending multiple columns to dataframe


I am creating a function which argument is a dataframe. Problem is that dataframes used by this function has different lengths, and I need my dataframe to have specific length in order my code to work.
To be specific, I need my dataframe to have 84 columns, and sometimes I have less columns (having more than needed 84 columns is not an issue). So in order my code to work I neeed to add columns filled with 0.
My column names are numbers starting from 0. I came up with this code:

df.insert(len(df.columns), len(df.columns)-1, 0)

but I am not sure how to put it into a loop to get dataframe which always has 84 columns?
Thank you.

Upvotes: 2

Views: 522

Answers (1)

sophocles
sophocles

Reputation: 13821

Assume a DF with 5 columns:

    A   B   C   D   F
0  92  58  84   4  63
1  44  28  58  97  27
2  51  75  76  16  39
3  42  32  85  98  32
4  98  90  65  78  85
5  32  16  31   5  95
6  88  69  23  37  12
7  52  13  57  57  90
8  60  49   5  15  14
9  43  40   1  76  17

[10 rows x 5 columns]

You could check the number of columns in your dataframe, and create new columns filled with 0 until you reach 84 columns:

new_cols = [f"column_new_{number}" for number in range(84 - len(df.columns))]
pd.concat([df, pd.DataFrame(columns = new_cols)]).fillna(0)

Which gives you:

Out[164]: 
      A     B     C  ...  column_new_76  column_new_77  column_new_78
0  92.0  58.0  84.0  ...              0              0              0
1  44.0  28.0  58.0  ...              0              0              0
2  51.0  75.0  76.0  ...              0              0              0
3  42.0  32.0  85.0  ...              0              0              0
4  98.0  90.0  65.0  ...              0              0              0
5  32.0  16.0  31.0  ...              0              0              0
6  88.0  69.0  23.0  ...              0              0              0
7  52.0  13.0  57.0  ...              0              0              0
8  60.0  49.0   5.0  ...              0              0              0
9  43.0  40.0   1.0  ...              0              0              0

[10 rows x 84 columns]

You can decide what the prefix of the columns can be, I used "column_new_" just for demonstration purposes.

Upvotes: 2

Related Questions