[英]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 应该是这样的
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.由于列数为 3,行数为 6,因此我想创建与行数一样多的虚拟列。 So in this case, I need 3 columns more, with all 0 values on them.
因此,在这种情况下,我还需要 3 列,其中所有值均为 0。 And I also want the column name is "d1", "d2", "d3", etc. Could anyone help me on this matter?
而且我还希望列名是“d1”、“d2”、“d3”等。有人可以帮我解决这个问题吗? Thanks in advance!
提前致谢!
You can do reindex
你可以做
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.