pandas - 在一个系列中划分元组
问题描述
如何划分熊猫系列:
d = {'a': ('a','ball'), 'b': ('b','cat'), 'c': ('c','plane')}
ser = pd.Series(data=d, index=['a', 'b', 'c'])
ser
a (a, ball)
b (b, cat)
c (c, plane)
dtype: object
进入一个熊猫数据框,如:
Index Value
a ball
b cat
c plane
解决方案
如果需要一列str[1]
,请使用:Series.to_frame
DataFrame
ser.str[1].to_frame('Value')
或者如果需要 2 列DataFrame
:
ser.str[1].rename_axis('Index').reset_index(name='Value')
推荐阅读
- rest - 同一设备中的两个应用程序的通信
- python - 函数返回不同类型是不好的做法吗?
- laravel-5 - SQLSTATE[42S22]: (SQL: select * from `categories` where `categories`.`departement_id` = 1 and `categories`.`departement_id` is not null)
- python - 在 python 中,如何在代码执行良好(退出代码 0)但没有结果(没有打印)时修复代码?
- javascript - 使用 Jquery 在幻灯片中添加下一个/上一个选项
- mysql - Laravel 悲观锁定不能防止在多个重复请求中对未提交的行进行 SELECT
- reactjs - 状态变量的值丢失 - React
- elasticsearch - 为搜索查询中的术语分配权重的弹性如何?
- python - 使用DataFrame矩阵的相关散点图?
- swift - 在 Xcode 中添加 AdMob