鱼C论坛

 找回密码
 立即注册
查看: 4436|回复: 17

[已解决]可以把series的值调换位置吗?

[复制链接]
发表于 2023-2-4 10:26:39 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-2-4 10:42 编辑

  1. import pandas as pd
  2. s = pd.Series([2065, 1120, 1044, 970, 908, 898, 652, 519])
  3. print("转换前:")
  4. print(s.info)

  5. print()
  6. #  转换的代码,实现原理是修改 pandas 用于格式化输出的源码把 fmt_index[1:] 和 fmt_values 调换,执行后永久生效,你可以看代码的实现过程,后面可以自己改回来
  7. import fileinput
  8. for line in fileinput.input(pd.io.formats.format.__file__, backup='.bak', inplace=1):
  9.     if line != '            result = self.adj.adjoin(3, *[fmt_index[1:], fmt_values])\n':
  10.         print(line, end='')
  11.     else:
  12.         print('            result = self.adj.adjoin(3, *[fmt_values, fmt_index[1:]])')
  13. pd.io.formats.format.__spec__.loader.load_module()
  14. print("转换后:")
  15. print(s.info)
复制代码


效果:
转换前:
<bound method Series.info of 0    2065
1    1120
2    1044
3     970
4     908
5     898
6     652
7     519
dtype: int64>

转换后:
<bound method Series.info of  2065   0
1120   1
1044   2
  970   3
  908   4
  898   5
  652   6
  519   7
dtype: int64>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-4 11:18:24 | 显示全部楼层
本帖最后由 isdkz 于 2023-2-4 11:20 编辑
skyhouse 发表于 2023-2-4 11:14
按您的代码,试运行下,运行后出现
转换前:


编码问题:加个参数encoding指定编码

修改后的代码:
  1. import pandas as pd
  2. s = pd.Series([2065, 1120, 1044, 970, 908, 898, 652, 519])
  3. print("转换前:")
  4. print(s.info)

  5. print()
  6. #  转换的代码,实现原理是修改 pandas 用于格式化输出的源码把 fmt_index[1:] 和 fmt_values 调换,执行后永久生效,你可以看代码的实现过程,后面可以自己改回来
  7. import fileinput
  8. for line in fileinput.input(pd.io.formats.format.__file__, backup='.bak', inplace=1, encoding='utf-8'):
  9.     if line != '            result = self.adj.adjoin(3, *[fmt_index[1:], fmt_values])\n':
  10.         print(line, end='')
  11.     else:
  12.         print('            result = self.adj.adjoin(3, *[fmt_values, fmt_index[1:]])')
  13. pd.io.formats.format.__spec__.loader.load_module()
  14. print("转换后:")
  15. print(s.info)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-4 12:03:42 | 显示全部楼层
本帖最后由 isdkz 于 2023-2-4 12:04 编辑
skyhouse 发表于 2023-2-4 11:58
请问怎么改回来格式化输出的源码,我执行其他py文件会报错。
刚刚重装了python跟pycharm,还是会报错。


会报错可能是因为你的pandas跟我的pandas版本不一致,所以代码也有点不一样

C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\formats\format.py

你把这个文件第 409 行(包括第 409 行前后的)内容复制出来给我看看

或者把报错信息放上来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-4 12:50:56 | 显示全部楼层
skyhouse 发表于 2023-2-4 12:42
format文件 399行开始
        if self.is_truncated_vertically:
            n_header_rows = 0


我之前的代码有做个备份的,你把
C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\formats\format.py 删掉
C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\formats\format.py.bak 这个文件的 .bak 去掉


然后你想把索引显示在右边的话就改下面标红的那一句,我就是把 fmt_values 和 fmt_index[1:] 的位置做了调换,你后面要改回来的话可以调换回来

format文件 399行开始
        if self.is_truncated_vertically:
            n_header_rows = 0
            row_num = self.tr_row_num
            row_num = cast(int, row_num)
            width = self.adj.len(fmt_values[row_num - 1])
            if width > 3:
                dot_str = "..."
            else:
                dot_str = ".."
            # Series uses mode=center because it has single value columns
            # DataFrame uses mode=left
            dot_str = self.adj.justify([dot_str], width, mode="center")[0]
            fmt_values.insert(row_num + n_header_rows, dot_str)
            fmt_index.insert(row_num + 1, "")

        if self.index:
            result = self.adj.adjoin(3, *[fmt_index[1:]], fmt_values)             # 这一句改成 result = self.adj.adjoin(3, *[fmt_values, fmt_index[1:]])   
        else:
            result = self.adj.adjoin(3, fmt_values)

        if self.header and have_header:
            result = fmt_index[0] + "\n" + result

        if footer:
            result += "\n" + footer

        return str("".join(result))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-4 12:53:25 | 显示全部楼层
本帖最后由 isdkz 于 2023-2-4 15:34 编辑
skyhouse 发表于 2023-2-4 12:51
这个方法比较伤筋动骨,有没其他方法?运行效率慢点也能接受。


暂时没有发现其它方法,那个改源码也就改一个地方而已,我就是怕你不会改才写了个代码的,

没想到那个代码跟你的 pandas 不通用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-4 16:51:33 | 显示全部楼层
suchocolate 发表于 2023-2-4 15:37
这种数据和index应该是在导入时解决的问题,不应该交给pandas.


他的意思大概是想把索引显示在右边,而不是把索引和数据调换
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-1 13:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表