1, pandas交换数据的两列

今天要对csv的数据进行列重拍序,有点费劲,还是用的不熟练

csv数据类似,交换db两列

         a     b    c       d  
0      196   242    3     881250949
1      186   302    3     891717742
2       22   377    1     878887116
3      244    51    2     880606923
4      166   346    1     886397596
import pandas as pd
import numpy as np

file = 'ppp.csv'
df = pd.read_csv(file, sep='\t', header=None ,names=['a'  , 'b' ,'c' ,'d'])
cols = list(df)
cols.insert(2,cols.pop(cols.index('d'))) # 2是将d放在哪一列,cols.pop(cols.index('d')) 是要换的d列
df = df.loc[:,cols] # 开始按照两列互换
print(df)

结果

         a     b          d  c
0      196   242  881250949  3
1      186   302  891717742  3
2       22   377  878887116  1
3      244    51  880606923  2
4      166   346  886397596  1

另外,其它用到的如下:

返回列数:df.shape[1]

返回行数:df.shape[0]   或者:len(df)

生成连续数字列表,并插入到某一列: 

a = df.shape[0]
b = list(range(a))
df.insert(0,'ind',pb.Series(b)) # 0是要插入的位置,‘ind’是表头,pb.Series(b)是要插入的值

例子

import glob,os
import regex as re
import pandas as pb
path = '/mnt/temp/tep/test/test'
result_csv = '/mnt/temp/tep/test/test_cp'
for file in glob.glob(os.path.join(path,'*')):

    df = None = None
    if file.endswith(".csv"):
        print(file)
        df = pb.read_csv(file, index_col=False)
        df.activateFlag_x[df.activateFlag_x==2] = 'aaa' #把activateFlag_x列等于2的值替换为aaa
        df.activateFlag_x[df.activateFlag_x == 1] = 'bbb' #同上
        #下面是将最右边的列通过逐次换到最左边
        cols = list(df)
        cols.insert(1, cols.pop(cols.index('activateFlag_x'))) #2和1换
        df = df.loc[:, cols]
        cols.insert(0, cols.pop(cols.index('activateFlag_x'))) #1和0换
        df = df.loc[:, cols]
        
        # a = df.shape[0]
        # b = list(range(a))
        # df.insert(0,'ind',pb.Series(b))
        
        # df.to_csv(os.path.join(result_csv,os.path.basename(file)),header=False,index=False) #去掉标题和最左边的索引id
        df.to_csv(os.path.join(result_csv, os.path.basename(file)), header=False) #只去掉头标题

    print(df)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值