我创建了一个数据框df
,如下所示:
dates counts1.22.171.gjj.csv counts4.16.8.iei.csv \
0 2003-01-01 0 0
1 2003-01-02 1 1
2 2003-01-03 0 0
3 2003-01-04 0 0
4 2003-01-05 0 0
5 2003-01-06 1 0
6 2003-01-07 0 0
7 2003-01-08 0 1
更多的行和列。我想将每一行的零和一加起来,这样我就得到了类似的东西:
dates counts
0 2003-01-01 0
1 2003-01-02 2
2 2003-01-03 0
3 2003-01-04 0
4 2003-01-05 0
5 2003-01-06 1
6 2003-01-07 0
7 2003-01-08 1
我使用df.sum(axis=1)
并成为
0 0
1 2
2 0
3 0
4 0
5 1
6 0
7 1
如何在第二列中有日期并且有标题的地方变成期望的输出?
答案 0 :(得分:0)
In [67]: df.assign(counts_sum=df.sum(axis=1))[["dates","counts_sum"]]
Out[67]:
dates counts_sum
0 2003-01-01 0
1 2003-01-02 2
2 2003-01-03 0
3 2003-01-04 0
4 2003-01-05 0
5 2003-01-06 1
6 2003-01-07 0
7 2003-01-08 1