用于计算某列中分布的确切数量的python是Dataframe
编写一个python程序来获取一个数据框(pandas)-“pre_data_matrix”,在这个数据框中有一列名为“PostTextPolarity”,它的值在-1和1之间,要计算大于0时的“PostTextPolarity”数,0是10000,小于0时的“PostTextPolarity”数可能是20000,我想获得准确的数字,程序是:用于计算某列中分布的确切数量的python是Dataframe,python,pandas,dataframe,Python,Pandas,Dataframe,编写一个python程序来获取一个数据框(pandas)-“pre_data_matrix”,在这个数据框中有一列名为“PostTextPolarity”,它的值在-1和1之间,要计算大于0时的“PostTextPolarity”数,0是10000,小于0时的“PostTextPolarity”数可能是20000,我想获得准确的数字,程序是: select_sql = "select userID,userName,userURL,postTime,postText,postTextLe
select_sql = "select userID,userName,userURL,postTime,postText,postTextLength,likesCount,sharesCount,commentsCount,postTextPolarity,postTextSubjectivity from fb_pre_davi_group_members_posts"
cur.execute(select_sql)
pre_data = cur.fetchall()
pre_data_list = list(pre_data )
...
pre_data_matrix = pd.DataFrame(pre_data_list,columns = ['userId','UserName','UserURL','PostTime','PostText','PostTextLength','LikesCount','SharesCount','CommentsCount','PostTextPolarity','PostTextSubjectivity'])
print(pre_data_matrix )
它表明:
LikesCount SharesCount CommentsCount PostTextPolarity \
0 0 0 0 0.0
1 0 0 0 0.3571428571428571
2 3 0 0 1.0
3 11 0 0 0.0
4 11 0 0 0.46909090909090906
5 0 0 0 0.9
6 11 0 1 0.625
7 11 0 1 0.0
8 11 0 0 0.56875
9 11 0 0 0.0
10 0 0 1 0.08333333333333333
11 20 0 2 0.0
12 4 0 1 0.0
13 7 0 1 0.0
14 11 0 1 0.25
...
您能告诉我如何获得PostTextPolarity>0、=0和的确切数量,并通过pandas库使用np.where:
g = pd.np.where(df.PostTextPolarity == 0,'Equals 0',pd.np.where(df.PostTextPolarity < 0,'< 0','> 0'))
df.groupby(g)['PostTextPolarity'].count().rename_axis('Category').reset_index()
花些时间观看本演讲并练习概念/示例。您的解决方案应该显而易见-请阅读并
Category PostTextPolarity
0 > 0 8
1 Equals 0 7