简体   繁体   English

如何在 Pandas 数据框中添加条目数作为新行?

[英]How can I add number of entries as a new row in pandas data frame?

I am working with Python and have a series which is as follows:我正在使用 Python 并且有一个系列如下:

            view_count    comment_count like_count   dislike_count  ratio_of_comments_per_view  ratio_of_likes_per_view
count      2.200000e+01     21.000000    22.000000      22.000000            21.000000          22.000000
mean       1.481812e+06     4547.523810  49981.863636   667.136364           0.002539            0.037818
std        2.263283e+06     8716.083952  79607.504617   1249.618086          0.001072            0.010861

After count, mean and std categories, I need a new row called number of entries which include number of entries for each group (number of entries for view count, number of entries for comment count etc.).在 count、mean 和 std 类别之后,我需要一个名为 number of entries 的新行,其中包括每个组的条目数(查看计数的条目数、评论计数的条目数等)。 Actually I could get number of entries by using .info() option and it gave me the following results:实际上我可以通过使用.info()选项获得条目数,它给了我以下结果:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 22 entries, 2 to 67
Data columns (total 8 columns):
title                         22 non-null object
view_count                    22 non-null int64
comment_count                 21 non-null float64
like_count                    22 non-null int64
dislike_count                 22 non-null int64
ratio_of_comments_per_view    21 non-null float64
ratio_of_likes_per_view       22 non-null float64
other_tag                     22 non-null object
dtypes: float64(3), int64(3), object(2)
memory usage: 1.5+ KB

But I do not know how to add these number of entries as a new row in my series.但我不知道如何在我的系列中添加这些条目作为新行。 Is there anyone who can help me with this problem?有没有人可以帮我解决这个问题?

My series should look like this:我的系列应​​该是这样的:

            view_count    comment_count like_count   dislike_count  ratio_of_comments_per_view  ratio_of_likes_per_view
count      2.200000e+01     21.000000    22.000000      22.000000            21.000000          22.000000
mean       1.481812e+06     4547.523810  49981.863636   667.136364           0.002539            0.037818
std        2.263283e+06     8716.083952  79607.504617   1249.618086          0.001072            0.010861
#entries         22                21         22         22                   21        22    

We can use DataFrame.count :我们可以使用DataFrame.count

For each column/row the number of non-NA/null entries.对于每一列/行,非 NA/空条目的数量。

If you want count by columns and add a new row:如果要按列计数并添加新行:

df=df.append(df.count().to_frame('entries').T)
print(df)

output for the sample dataframe:示例数据帧的输出:

         view_count  comment_count    like_count  dislike_count  \
count          22.0      21.000000     22.000000      22.000000   
mean      1481812.0    4547.523810  49981.863636     667.136364   
std       2263283.0    8716.083952  79607.504617    1249.618086   
entries         3.0       3.000000      3.000000       3.000000   

         ratio_of_comments_per_view  ratio_of_likes_per_view  
count                     21.000000                22.000000  
mean                       0.002539                 0.037818  
std                        0.001072                 0.010861  
entries                    3.000000                 3.000000  

If you want count by rows and create a new column:如果您想按行计数并创建一个新列:

df['entries']=df.count(axis=1)
print(df)

Output:输出:

       view_count  comment_count    like_count  dislike_count  \
count        22.0      21.000000     22.000000      22.000000   
mean    1481812.0    4547.523810  49981.863636     667.136364   
std     2263283.0    8716.083952  79607.504617    1249.618086   

       ratio_of_comments_per_view  ratio_of_likes_per_view  entries  
count                   21.000000                22.000000        6  
mean                     0.002539                 0.037818        6  
std                      0.001072                 0.010861        6 

You can use this line:您可以使用这一行:

df['new_col'] = df.notnull().sum(axis=1)

It gives you the number of non-null values per row (or did you want per column?).它为您提供每行非空值的数量(或者您是否想要每列?)。 If you have 4 rows:如果您有 4 行:

Out[87]: 
0    6
1    5
2    6
3    6
dtype: int64

看看这个链接www.geeksforgeeks.org/python-pandas-dataframe-sum/amp/。

暂无
暂无

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

相关问题 如何将列的一部分添加到新的 Pandas 数据框中? - How can I add parts of a column to a new pandas data frame? 创建一个新列,该列是一行中有多少条目满足pandas中数据帧的每一行条件的计数 - Create new column that is a count of how many entries in a row satisfy a condition for each row of a data frame in pandas 如何在包含现有行字符串中的单词的pandas数据框中创建新行? - How can I create new rows in a pandas data frame containing the words in a string of an existing row? 如何仅在 Pandas 中将列数据添加到第一行或为新列条目提供默认数据? - How to add column data to the first row only in pandas OR provide default data for new column entries? 如何将熊猫数据框添加到每一行? - How to add pandas data frame to each row? 如何将熊猫数据框的第 n 行提取为熊猫数据框? - How can I extract the nth row of a pandas data frame as a pandas data frame? 如何在 pandas dataframe 中的每个唯一条目之后添加一个新行 - How to add a new row after every unique entries in pandas dataframe pyspark添加带有数据框行号的新列字段 - pyspark add new column field with the data frame row number 如何使用从当前行和上一行访问数据的 function 向数据框添加新列? - How can I add a new column to a data frame using a function that accesses data from both current and previous row? 如何在 Pandas 数据框中添加一个级别 - How to i add a level in pandas data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM