![](/img/trans.png)
[英]Count number of consecutive rows that are greater than current row value but less than the value from other column
[英]Adding Dummy Column If Number of Column is less than number of rows
我有一個看起來像這樣的非方桌:
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
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
由於列數為 3,行數為 6,因此我想創建與行數一樣多的虛擬列。 因此,在這種情況下,我還需要 3 列,其中所有值均為 0。 而且我還希望列名是“d1”、“d2”、“d3”等。有人可以幫我解決這個問題嗎? 提前致謝!
你可以做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.