python - 不同长度的熊猫系列
问题描述
使用 pandas concat 函数可以创建如下系列:
In[230]pd.concat({'One':pd.Series(range(3)), 'Two':pd.Series(range(4))})
Out[230]:
One 0 0
1 1
2 2
Two 0 0
1 1
2 2
3 3
dtype: int64
不使用 concat 方法是否可以做同样的事情?我最好的方法是:
a = pd.Series(range(3),range(3))
b = pd.Series(range(4),range(4))
pd.Series([a,b],index=['One','Two'])
但它不一样,它输出:
One 0 0
1 1
2 2
dtype: int64
Two 0 0
1 1
2 2
3 3
dtype: int64
dtype: object
解决方案
这应该让您了解它的用处concat
。
a.index = pd.MultiIndex.from_tuples([('One', v) for v in a.index])
b.index = pd.MultiIndex.from_tuples([('Two', v) for v in b.index])
a.append(b)
One 0 0
1 1
2 2
Two 0 0
1 1
2 2
3 3
dtype: int64
同样的事情是通过pd.concat([a, b])
.
推荐阅读
- c++ - 在 QT for MacOs 和 windows 中创建安装程序的过程
- python - 如何在 Python 中为内核创建各向异性指数和高斯相关函数?
- javascript - 空 div 但仍然在前端加载内容
- google-apps-script - 如何根据列匹配合并两行。(Google 电子表格)?
- intellij-idea - 缺少键盘映射选项 Next TODO
- python-3.x - 预先突出显示的日期 KivyMD DatePicker
- sql-server - 无法使用 Python 字典作为列值执行 SQL 插入命令
- c++ - 从 C 字符串与从另一个 std::string 构造 std::string 的不一致
- nginx - 为什么 Nginx 中的 proxy_pass 不处理子 uri?
- salesforce - 有没有一种方法可以在 Salesforce 中识别沙盒的许可证类型?(Developer、Developer Pro、部分复制、完整)