简体   繁体   中英

Create multiple empty columns and assign it to 0 in pandas dataframe

I am not sure if it's a good idea. I am using transfer learning to train some new data. The model shape has 180 columns(features) and the new data input has 500 columns. It 's not good to cut columns from the new data. So I am thinking to add more columns to the dataset used in the original model. So if I want to add eg columns from 181 to 499 and assign 0 to those cells, how can I do it? Please ignore label column now. Thanks for your help

Original df:

     0          1        2        3       4        5 ... 179 (to column 179) label
 0   0.28001 0.32042  0.93222. 0.87534. 0.44252 0.2321
 1
 2

 Expected output
     0          1        2        3       4        5 ... 179 180 181 182 ....499 label
 0   0.28001 0.32042  0.93222. 0.87534. 0.44252 0.2321    0   0   0   0      0
 1   0.38001 0.42042  0.13222. 0.67534. 0.64252 0.4321    0   0   0   0      0
 2

Since you don't care about columns label, use pd.concat on new construct dataframe from np.zeros

Sample df

In [336]: df
Out[336]:
         0        1         2         3        4       5
0  0.28001  0.32042  0.93222.  0.87534.  0.44252  0.2321
1  0.38001  0.42042  0.13222.  0.67534.  0.64252  0.4321

m = 20  #use 20 to show demo. You need change it to 500 for your real data
x, y  = df.shape

df_final = pd.concat([df, pd.DataFrame(np.zeros((x, m - y))).add_prefix('n_')], axis=1)

In [340]: df_final
Out[340]:
         0        1         2         3        4       5  n_0  n_1  n_2  n_3  \
0  0.28001  0.32042  0.93222.  0.87534.  0.44252  0.2321  0.0  0.0  0.0  0.0
1  0.38001  0.42042  0.13222.  0.67534.  0.64252  0.4321  0.0  0.0  0.0  0.0

   n_4  n_5  n_6  n_7  n_8  n_9  n_10  n_11  n_12  n_13
0  0.0  0.0  0.0  0.0  0.0  0.0   0.0   0.0   0.0   0.0
1  0.0  0.0  0.0  0.0  0.0  0.0   0.0   0.0   0.0   0.0

If you need columns in sequential numbers

m = 20
x, y  = df.shape

df_final = pd.concat([df, pd.DataFrame(np.zeros((x, m - y)), columns=range(y, m))], axis=1)

Out[341]:
         0        1         2         3        4       5    6    7    8    9  \
0  0.28001  0.32042  0.93222.  0.87534.  0.44252  0.2321  0.0  0.0  0.0  0.0
1  0.38001  0.42042  0.13222.  0.67534.  0.64252  0.4321  0.0  0.0  0.0  0.0

    10   11   12   13   14   15   16   17   18   19
0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM