[英]Merged Columns in Python Data Frame
How can I make this table:我怎样才能制作这张桌子:
into a Pandas data frame?进入 Pandas 数据帧? Can't make that Machine Column.不能制作那个机器专栏。
You can't really do that in a dataframe, as you can't have a one-level index combined with a multi-level index on the same axis.您不能在 dataframe 中真正做到这一点,因为您不能在同一轴上将单级索引与多级索引结合使用。
One way to get as close as possible to what you want is to concatenate individual pandas series for the first one-level columns with a two-level dataframe for the 'machine' columns like follows:尽可能接近您想要的一种方法是将第一个一级列的单个 pandas 系列与“机器”列的两级 dataframe 连接起来,如下所示:
pd.concat({
'Company name': pd.Series(['a', 'b', 'c']),
'Number of machines': pd.Series([1, 4, 2]),
'Machines': pd.DataFrame({
'2015-2020': pd.Series([3, 1, 0]),
'2018-2014': pd.Series([1, 8, 3]),
'Other': pd.Series([5, 0, 4]),
})
}, axis=1)
You will still a two-level index as a result, and the first columns will have a 2nd level integer index (0, 1 etc.)结果仍然是两级索引,第一列将有第二级 integer 索引(0、1 等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.