[英]Python: Add Count of Rows from df to Another df as New Column
Let's say I counted the number of rows in a pandas dataframe. 假设我计算了熊猫数据框中的行数。 I use the following code to do that:
我使用以下代码来做到这一点:
df.shape
It gives me the following result: (1700, 12) 它给了我以下结果:(1700,12)
How do I add the 1700 value to an existing pandas dataframe? 如何将1700值添加到现有的熊猫数据框? We'll call the column associated with that value D.
我们将与该值D关联的列称为。
Current dataframe: 当前数据框:
A B C
30 40 text
Desired dataframe: 所需的数据框:
A B C D
30 40 text 1700
you store your shape in a variable of type tuple and then you create a new column and paste the first value of the tuple 您将形状存储在元组类型的变量中,然后创建一个新列并粘贴元组的第一个值
tab = df.shape
df['D'] = tab[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.