熊猫:创建新的数据框,其中包含总行数的列

时间:2018-07-19 01:26:38

标签: pandas dataframe count rows

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

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

ID  Total Rows
73Y 601
73L 391

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

1 个答案:

答案 0 :(得分:0)

.shape方法返回(行,列)形式的元组。您可以分别使用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]})