Pandas:如果轴选项卡的长度大于4,则中值

2024-06-16 11:21:20 发布

您现在位置:Python中文网/ 问答频道 /正文

如果每个季度每个行业的数据点数量少于4个,我会尝试让NS显示出来。在

以下是我的数据源:

print df

                        IndustryGroup  Year_  Quarter  TotalRaisedUSD
                            Healthcare  2014_        1        79.30000
                     Consumer Services  2014_        1       111.25000
                Information Technology  2014_        1       232.00000
                            Healthcare  2014_        1       113.90000
                            Healthcare  2014_        1       121.11000
                            Healthcare  2014_        1       108.76000
                            Healthcare  2014_        1        58.64000
                            Healthcare  2014_        1       120.07000
                            Healthcare  2014_        1        81.76000
                            Healthcare  2014_        1        28.40000
                            Healthcare  2014_        1        76.63000
                            Healthcare  2014_        1        74.96217
                            Healthcare  2014_        1        57.86000
                            Healthcare  2014_        1       220.23000
                                ...     ...         ...       ...
                            Healthcare  2014_        4       109.70000
                     Consumer Services  2014_        4       358.50000
                            Healthcare  2014_        4       115.00000
                Information Technology  2014_        4       168.05000
       Business and Financial Services  2014_        4       100.66000
                            Healthcare  2014_        4        72.09000
                            Healthcare  2014_        4       129.67000
                            Healthcare  2014_        4       151.00000
                            Healthcare  2014_        4       123.28000
                            Healthcare  2014_        4       153.85000
       Business and Financial Services  2014_        4        47.41000
                            Healthcare  2014_        4        35.34000
                            Healthcare  2014_        4       206.50000
                            Healthcare  2014_        4        31.00000
                            Healthcare  2014_        4        68.09000
                            Healthcare  2014_        4       122.02000
       Business and Financial Services  2014_        4       193.22000
                Information Technology  2014_        4       254.34000
                Information Technology  2014_        4       196.50000


df1=pd.pivot_table(df,values='TotalRaisedUSD',index='IndustryGroup',columns=['Year_','Quarter'], np.median)

我需要相同的df1输出,但是如果TotalRaisedUSD的计数小于4,则显示“NS”(例如,Information Technology 2014_4应该显示NS)

^{pr2}$

有什么想法吗?在

谢谢!!在


Tags: anddfinformationconsumerbusinessyeardf1ns
1条回答
网友
1楼 · 发布于 2024-06-16 11:21:20

您可以使用groupbytransform来完成此操作:

grouped = df.groupby(['IndustryGroup','Year'])

logic= lambda x: 'NS' if x.count() < 4 else '' 

transformed = grouped.transform(logic)

我相信这样的事情应该行得通。在

你可以在这里读到: http://pandas.pydata.org/pandas-docs/stable/groupby.html

相关问题 更多 >