我有两个数据帧,每个看起来像
date country value
20100101 country1 1
20100102 country1 2
20100103 country1 3
date country value
20100101 country2 4
20100102 country2 5
20100103 country2 6
我想把它们合并成一个数据帧
date country1 country2
20100101 1 4
20100102 2 5
20100103 3 6
有什么聪明的方法可以在熊猫身上做到这一点吗?你知道吗
这看起来像pivot表,在Pandas中由于一些奇怪的原因被称为
unstack
。你知道吗类似于Wes McKinley的“python for data analysis”一书中使用的示例:
(
groupby
在行中的操作系统,然后将其旋转,使操作系统成为列,就像您的“country*”值一样)。你知道吗另外,对于
cat
数据帧,您可以使用pandas.concat
。对生成的数据帧执行.reset_index
也是很好的,在某些(许多?)中是bc案例索引中的重复值会使pandas失控,在dataframe上使用的.apply
上抛出奇怪的异常。你知道吗相关问题 更多 >
编程相关推荐