简体   繁体   English

我们如何计算数据框中的项目并将结果分配给数据框中的新列?

[英]How can we do counts of items in a data frame and asign results to a new column in the dataframe?

I have street addresses that look like this.我有看起来像这样的街道地址。

250 EAST HOUSTON STREET
211 EAST 3RD STREET
182 EAST 2ND STREET
511 EAST 11TH STREET
324 EAST 4TH STREET
324 EAST 4TH STREET
324 EAST 4TH STREET
324 EAST 4TH STREET
324 EAST 4TH STREET
324 EAST 4TH STREET
324 EAST 4TH STREET
324 EAST 4TH STREET
754 EAST 6TH STREET

How can I get counts, in the same data frame, like this?我怎样才能在同一个数据框中获得计数,像这样?

250 EAST HOUSTON STREET 3
211 EAST 3RD STREET     1
182 EAST 2ND STREET     1
511 EAST 11TH STREET    1
324 EAST 4TH STREET     8
324 EAST 4TH STREET     8
324 EAST 4TH STREET     8
324 EAST 4TH STREET     8
324 EAST 4TH STREET     8
324 EAST 4TH STREET     8
324 EAST 4TH STREET     8
324 EAST 4TH STREET     8
754 EAST 6TH STREET     1

The name of the field that I want to count is 'Street'.我要计算的字段名称是“街道”。 I found some code that counts dupes, but it does a group by and takes everything into a new data frame.我发现了一些计算欺骗的代码,但它进行了分组并将所有内容都放入一个新的数据框中。 I want to assign counts to a new column in the same data frame.我想将计数分配给同一数据框中的新列。 Thanks!谢谢!

Use groupby + transform .使用groupby + transform Transform allows you to call the series to a new column.转换允许您将系列调用到新列。 If you do not use transform, then you have a consolidated series that is a mismatch for the dataframe and your column will instead be filled with NaN values:如果您不使用转换,那么您有一个与数据框不匹配的合并系列,您的列将改为填充NaN值:

import pandas as pd
# df = pd.read_clipboard('\s\s+', header=None).rename({0: 'Street'}, axis=1) # how I read in your data from your StackOverflow question
df['Count'] = df.groupby('Street')['Street'].transform('count')
df
Out[1]: 
                     Street  Count
0   250 EAST HOUSTON STREET  1
1       211 EAST 3RD STREET  1
2       182 EAST 2ND STREET  1
3      511 EAST 11TH STREET  1
4       324 EAST 4TH STREET  8
5       324 EAST 4TH STREET  8
6       324 EAST 4TH STREET  8
7       324 EAST 4TH STREET  8
8       324 EAST 4TH STREET  8
9       324 EAST 4TH STREET  8
10      324 EAST 4TH STREET  8
11      324 EAST 4TH STREET  8
12      754 EAST 6TH STREET  1

I'm really new to trying to help people on this platform so feel free to tell me I'm not looking in the right places for the info, but are you using Pandas or another library?我真的很陌生,试图在这个平台上帮助人们,所以请随时告诉我我没有在正确的地方寻找信息,但是你使用的是 Pandas 还是其他图书馆? If you're using Pandas I think there's a method called valuecount (maybe value_count) that could be useful.如果您使用的是 Pandas,我认为有一种称为 valuecount(可能是 value_count)的方法可能很有用。 Sorry I can't be more helpful but I'm learning the ropes here.对不起,我不能提供更多帮助,但我正在学习这里的绳索。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何计算数据框列中的重复数据并将结果分配给同一数据框中的新列? - How can we count dupes in a column of a data frame and assign the results to a new column in the same data frame? 我将如何创建一个具有 aa 列的唯一值并对其进行计数的新数据框? - How would I go about creating a new data frame that has the unique values of a a column and it counts them? 如何将计算结果添加到数据框中的新列? - How do I add the results from a calculation to a new column in a dataframe? 具有唯一 groupby 的新列在数据框中产生 - New column with unique groupby results in data frame 我们如何在数据框中的特定列中找到最后一行? - How can we find the last row, in a specific column, in a data frame? 如何使用计数将数据帧重新采样到新列中并将列聚合到列表中 - How to resample dataframe with counts into new column and aggregate column into list 我们如何在数据框中的列中的字符串周围添加引号 - How can we add quotes around a string in a column in a data frame 需要在现有数据框的一列中创建仅具有奇数个值的新数据框 - Need to make new data frame with only odd counts of a value in a column of existing data frame 如何通过一列的值计数对熊猫数据框进行排序? - How to sort a pandas data frame by value counts of a column? 我们可以根据使用 python 的索引将一个数据帧中一列的值插入另一个 dataframe 的另一列吗? - Can we insert values of one column from one data frame to another column of another dataframe based on index using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM