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