Daniel Slätt
Daniel Slätt

Reputation: 771

Create a new row with zeroes in wide Pandas DataFrame

I'm trying to fill an empty array with one row of zeroes. This is apparently a lot harder said than done. This is my attempt:

Array = pd.DataFrame(columns=["curTime", "HC", "AC", "HG", "HF1", "HF2", "HF3", "HF4", "HF5", "HF6", 
                            "HF7", "HF8", "HF9", "HF10", "HF11", "HF12", "HD1", "HD2", "HD3", "HD4", "HD5", "HD6",
                            "AG", "AF1", "AF2", "AF3", "AF4", "AF5", "AF6", "AF7", "AF8", "AF9", "AF10", "AF11", "AF12",
                            "AD1", "AD2", "AD3", "AD4", "AD5", "AD6"])
appendArray = [[0] * len(Array.columns)]
Array = Array.append(appendArray, ignore_index = True)

This however creates a row that stacks another 41 columns to the right of my existing 41 columns, and fills them with zeroes, while the original 41 columns get a "NaN" value.

How do I most easily do this?

Upvotes: 0

Views: 33

Answers (1)

BENY
BENY

Reputation: 323226

You can using pd.Series within the append

Array.append(pd.Series(appendArray,index=Array.columns), ignore_index = True)
Out[780]: 
   curTime  HC  AC  HG  HF1  HF2  HF3  HF4  HF5  HF6 ...   AF9  AF10  AF11  \
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   
   AF12  AD1  AD2  AD3  AD4  AD5  AD6  
0     0    0    0    0    0    0    0  
1     0    0    0    0    0    0    0  
[2 rows x 41 columns]

Upvotes: 1

Related Questions