python交换数据的两列,python处理excel数据, python交换
python交换数据的两列,python处理excel数据, python交换
python交换数据的两列有两种方式:
第一种:在numpy中交换数据的两列;
上代码:
1 import numpy as np2 a = np.array([[1,2,3],[4,5,6]])3 >>> a4 array([[1, 2, 3],5 [4, 5, 6]])6 >>> a[:,[0, -1]] = a[:,[-1, 0]]7 >>> a8 array([[3, 2, 1],9 [6, 5, 4]])
第二种:在pandas中交换数据的两列;以movieLen100K中的u.data为例;
上代码:
1 import pandas as pd 2 import numpy as np 3 4 file = ‘ml-100k/u.data‘ 5 df = pd.read_csv(file, sep=‘\t‘, header=None ,names=[‘a‘ , ‘b‘ ,‘c‘ ,‘d‘]) 6 print(df) 7 cols = list(df) 8 cols.insert(2,cols.pop(cols.index(‘d‘))) 9 df = df.loc[:,cols]10 print(df)
测试结果:
a b d c0 196 242 881250949 31 186 302 891717742 32 22 377 878887116 13 244 51 880606923 24 166 346 886397596 1
很明显,‘d’与‘c’交换了位置;
至此,python中的数据交换位置讲完;
python交换数据的两列
相关内容
- 关于python,你有什么想要知道的吗?,python写我爱你的代
- FOR循环深度剖析,学Python为什么别人是技术总监而你只
- Python爬取51job职位信息,,# -*- codi
- 你真的会使用 Python 命令吗?,linux常用的20个命令
- 我发现了个 Python 黑魔法,执行任意代码都会自动念上
- Python3 upper()方法,python中lower什么意思,描述Python u
- 10条很棒的Python一行代码,棒条振动筛
- Python 常用import,pythonimport函数,import rei
- python中的urlencode和urldecode,pythonurlencode,python将字符串
- 记一次Python Web接口优化,性能提升25倍!,接口性能优
评论关闭