Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding Dummy Column If Number of Column is less than number of rows

I have non-square table which look like this:

input:
        A   B   C
    0   4   2   5 
    1   2   6   8
    2   8   3   4
    3   4   2   5
    4   3   6   7
    5   7   3   8

the output should be like this

output:
        A   B   C   d1   d2   d3
    0   4   2   5   0    0    0
    1   2   6   8   0    0    0
    2   8   3   4   0    0    0
    3   4   2   5   0    0    0
    4   3   6   7   0    0    0
    5   7   3   8   0    0    0

Since the number of columns is 3 and the rows are 6, therefore I want to create dummy columns as much as the number of rows. So in this case, I need 3 columns more, with all 0 values on them. And I also want the column name is "d1", "d2", "d3", etc. Could anyone help me on this matter? Thanks in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do reindex

out = df.reindex(columns = df.columns.to_list()+[*range(df.shape[0]-df.shape[1])],fill_value=0)
Out[65]: 
   A  B  C  0  1  2
0  4  2  5  0  0  0
1  2  6  8  0  0  0
2  8  3  4  0  0  0
3  4  2  5  0  0  0
4  3  6  7  0  0  0
5  7  3  8  0  0  0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading