- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
最佳答案
一种方法是使用 set_index
、reindex
使用 pd.MultiIndex.from_product
和 将“年份”设置为另一级索引>reset_index
将“year”中的数据作为列。
具有相同结构的示例数据框:
import pandas as pd
df = pd.DataFrame( {'year':[2003,2004,2002,2004,2005,2006],
'city_code':['a']*2+['b']*4,
'total_tax':pd.np.random.randint(100,1000,6)},
index=pd.Index(data=[9]*2+[54]*4,name='id_inf'))
print(df)
city_code total_tax year
id_inf
9 a 417 2003
9 a 950 2004
54 b 801 2002
54 b 218 2004
54 b 886 2005
54 b 855 2006
现在您可以使用以下方法创建df_balanced
:
df_balanced = (df.set_index('year',append=True)
.reindex(pd.MultiIndex.from_product([df.index.unique(),
range(df.year.min(),df.year.max()+1)],
names=['id_inf','year']))
.reset_index(level=1))
你会得到:
print (df_balanced)
year city_code total_tax
id_inf
9 2002 NaN NaN
9 2003 a 417.0
9 2004 a 950.0
9 2005 NaN NaN
9 2006 NaN NaN
54 2002 b 801.0
54 2003 NaN NaN
54 2004 b 218.0
54 2005 b 886.0
54 2006 b 855.0
要填充NaN
,有不同的方法,但这里有两种方法。对于“city_code”列,您可以使用 groupby
和 transform
与 max
来获取值,对于“total_tax”列,只需 fillna
用 0 例如:
df_balanced['city_code'] = df_balanced.groupby(level=0)['city_code'].transform(max)
df_balanced['total_tax'] = df_balanced['total_tax'].fillna(0)
print (df_balanced)
year city_code total_tax
id_inf
9 2002 a 0.0
9 2003 a 417.0
9 2004 a 950.0
9 2005 a 0.0
9 2006 a 0.0
54 2002 b 801.0
54 2003 b 0.0
54 2004 b 218.0
54 2005 b 886.0
54 2006 b 855.0
关于Python Pandas : Balance an unbalanced dataset (for panel analysis),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53439133/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
查询 Analysis Services 的服务器版本信息,是否有任何明确的方法可以确定您访问的服务器是 Azure Analysis Services 托管服务还是您访问的是在 Azure 中的 V
嗨,我正在使用Travis CI for Maven应用程序在SonarCloud上自动执行代码覆盖。 现在,在本地运行sonar:sonar命令在SoundCloud上提交报告,我可以通过Branc
我已经将我的项目从 Vite 2.x 更新到 Vite 3.0.2,突然出现这个错误: [plugin:vite:import-analysis] Failed to parse source for
我怎么知道我是否过度分析了? 过去 3 天我一直在追寻一个问题。我经历过许多设计并使用大约 3 个类得出了一个复杂的解决方案。与同事讨论后,我意识到我只需要一个方法和一个struct。我怎样才能避免成
我有不同的供应商,它们通过不同城市向我传递了一个 Excel,在每个城市,他们使用一些特殊的代码进行他们的操作和更多对我的业务有用的数据。 问题是我对所有这些城市都一团糟: 我的数据库中有自己的城市,
我认为这可能是一个关于大 O 符号的初学者问题。举例来说,我有一个算法,它递归地分解整个列表(O(n)),然后将其重新组合在一起(O(n))。我假设这意味着效率是 O(n) + O(n)。这是否简化为
我一直在挑战维基百科 pageviews分析。对我来说,这是第一个拥有如此大量数据的项目,我有点迷茫。当我从链接下载文件并将其解压缩时,我可以看到它具有类似表格的结构,其中的行如下所示: 1 |
我刚刚偶然发现了一份为我公司的一位客户(SMB)分析项目的工作。我是一名 .Net 开发人员(5 年),在分析软件方面经验很少(来自学校的 UML)。 我们刚刚结束了与用户两周的交谈,并得到了一份很好
对于准确的 Web 日志分析工具来生成 IIS 日志报告有什么建议吗?我们使用了 WebTrends,但我认为它不准确。 最佳答案 要分析博客,我认为使用模拟不会出错:http://www.analo
在寻找专门针对 K-map 最优性的文献时,我将不胜感激。 例如,我了解如何在 SOP(乘积和)表达式和 K-map 之间进行映射,以及为什么通常您希望 K-map 优化表达式更简单,因为找到了 1
首先,我在发布这个问题之前已经进行了搜索。我已经看过这个问题 Why is quicksort better than mergesort?但它有一些相互矛盾的答案。 根据我的观察,人们说快速排序比合
我有兴趣使用像 OllyDbg 这样的东西来检查一个可能具有类似木马特征的程序。 有谁知道关于如何安全地做到这一点的任何好的教程? 基本上该程序是一个视频游戏的“机器人”,但我怀疑它有后门和/或将它收
我可以对单个属性使用 k-means 算法吗? 属性和簇数有关系吗? 我有一个属性的性能,我想将数据分为 3 个集群:差、中和好。 是否可以用一个属性创建 3 个集群? 最佳答案 K-Means 在您
现在我对符号执行(SE)和可达性分析(RA)感到困惑。据我所知,SE使用符号来执行一些代码以到达具有分支条件的每个分支。而RA可以用来求每个分支的可达性,对吧?当使用 RA 时,我们可以提取每个分支的
假设我有一个 ActivePivot 多维数据集,其中的事实仅包含值和货币。假设我的多维数据集将货币作为常规维度。 我们用具有多种货币的事实填充立方体。 我们有一个外汇服务,它使用货币和引用货币来计算
在Weka中使用Kmeans时,可以在模型的结果输出上调用getAssignments()以获取每个给定实例的集群分配。这是一个(截断的)Jython示例: >>>import weka.cluste
确切地说,我有两个问题。首先,我想知道是否有一种简单的方法来适应马尔可夫聚类算法,以便我可以提前指定最后我想要有多少个聚类。如果没有,您会推荐哪种类似的算法? 其次应该如何处理马尔可夫世界中的重叠集群
在进行情感分析时,如何让机器理解我指的是苹果(iphone),而不是苹果(水果)? 谢谢你的建议! 最佳答案 嗯,有几种方法, 我会从检查大写字母开始,通常,当提到一个名字时,第一个字母是大写的。 在
我在学习非线性聚类算法时遇到了这个二维图。我想知道哪种聚类算法和超参数的组合可以很好地聚类这些数据。 就像人类将这 5 个尖峰聚集在一起一样。我希望我的算法能够做到这一点。 我尝试了 KMeans,但
我是一名优秀的程序员,十分优秀!