首页 > 解决方案 > Pandas:创建包含总行列的新数据框

问题描述

我有以下数据框:df_Discrep_73L_3,共有 601 行我有第二个数据框:df_Discrep_73Y_3,共有 391 行

我想创建一个具有以下内容的数据框:

ID  Total Rows
73Y 601
73L 391

我知道使用 df_Discrep_73L_3.shape 来查找行数,但不知道如何将其放入上述数据框中。

标签: pandasdataframecountrows

解决方案


.shape 方法以 (rows,columns) 形式返回一个元组。您可以分别使用 shape[0] 和 shape[1] 来保存它们。

rows_73L = df_Discrep_73L_3.shape[0]
rows_73Y = df_Discrep_73Y_3.shape[0] 

pd.DataFrame({'ID': ["73Y", "73L"], 'Total Rows': [rows_73Y, rows_73L]})

推荐阅读