简体   繁体   English

Python:将df的行数添加到另一个df作为新列

[英]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.

相关问题 你能帮我从 pandas df 中找到一行内容到另一个 df 中,然后将发现的计数添加到第一个 df 的新列中吗? - Can you help me finding a row content from a pandas df into another df and then add the count of the findings into a new column of the first df? 根据另一个df中的列计算一个df中的行数 - count number of rows in one df depending on column in another df 根据另一个df中与熊猫相关的信息,在另一个df中添加新列 - Add a new column in another df depending on informations in another df with pandas 如何根据条件从另一行的字符串创建新的df列? - How to create new df column from strings of another rows with conditions? Python-如果存在于另一个df列中,则从df列中删除元组 - Python - Remove tuple from df column if present in another df column 根据另一个 df 的列删除 df 行 - Remove df rows based on column of another df 有没有办法匹配来自两个数据帧的序列号并将 df2 中的系列(来自行)列表添加到 df1 的新列中(Python,pandas) - Is there a way to match serial numbers from two dataframes and add a list of Series (from rows) from df2 into a new column in df1 (Python, pandas) Pandas将行从1 DF移动到另一个DF - Pandas move rows from 1 DF to another DF 从 Df 创建新表,显示一列的计数和另一列的算术 - Create New table from Df that shows count of one column and a arithmetic of another column Python Pandas DF-组列,另一列具有相应的频率计数 - Python Pandas DF - Group column with corresponding frequency count of another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM