码迷,mamicode.com
首页 > 其他好文 > 详细

修改读入的CSV文件中的列名

时间:2018-04-10 17:47:42      阅读:1118      评论:0      收藏:0      [点我收藏+]

标签:pen   api   文件中   code   taf   ict   column   panda   change   

df.rename的用法

存在这样一个CSV文件,想读入文件后,将其列名进行修改,并再写到CSV中.

日期,收盘changeColName,涨跌幅,换手率,振幅,内盘成交量,外盘成交量,金额流入率,大单流入资金,大单流出资金,超大单流入资金,超大单流出资金,大单金额流入率,超大单金额流入率
2010-01-04,47.3,-3.272,0.69437708,0.037423313,,,-8.099998782,56272128,63797540,1235515,2807990,-6.699998366,-1.400000416
2010-01-05,46.65,-1.3742,1.009538457,0.026638478,,,-7.000001914,84605408,97264176,16183990,14741852,-7.900002071,0.900000157
2010-01-06,46.84,0.4073,0.941143214,0.019506967,,,-1.199996446,72186448,86293568,44122280,31816064,-9.399997926,8.20000148
2010-01-07,45.85,-2.1136,0.553292649,0.023270709,,,0.799993095,41697248,48400152,12012986,4613683,-7.700006626,8.499999721
2010-01-08,46.69,1.8321,0.891040511,0.033805889,,,1.800001548,52810168,49703684,55069420,55634236,2.200002522,-0.400000974

python代码如下:

# -*- coding: UTF-8 -*-
import numpy as np
import pandas as pd

colName = {
	‘日期‘   : ‘Date‘,
	‘开盘价‘ : ‘Open‘,
	‘收盘价‘ : ‘Close‘,
	‘最高价‘ : ‘High‘,
	‘最低价‘ : ‘Low‘,
	‘涨跌‘   : ‘UpPrice‘,
	‘涨跌幅‘ : ‘UpPercent‘,
	‘三日均涨幅‘ : ‘MUpPert3‘,
	‘五日均涨幅‘ : ‘MUpPert5‘,
	‘十日均涨幅‘ : ‘MUpPert10‘,
	‘成交量‘ : ‘Volumn‘,
	‘振幅‘   : ‘Amplitude‘,
	‘换手率‘ : ‘TurnOver‘,
	‘内盘成交量‘: ‘BuyVol‘,
	‘外盘成交量‘ : ‘SellVol‘,
	‘金额流入率‘ : ‘CapitalInRate‘,
	
	‘大单流入资金‘ : ‘BigCapitalIn‘,	
	‘大单流出资金‘ : ‘BigCapitalOut‘,	
	‘大单金额流入率‘ : ‘BigCapitalInRate‘,	
	
	‘超大单流入资金‘ : ‘SuperBigCapitalIn‘,	
	‘超大单流出资金‘ : ‘SuperBigCapitalOut‘,		
	‘超大单金额流入率‘ : ‘SuperBigCapitalInRate‘,

	‘大单买盘成交量‘ : ‘BigBuyVolumn‘,
	‘大单卖盘成交量‘ : ‘BigSellVolumn‘,
	‘超大单买盘成交量‘ : ‘SuperBigBuyVolumn‘,
	‘超大单卖盘成交量‘ : ‘SuperBigSellVolumn‘
}


def write_to_csv(filePath, date, test_x, test_y):
    index = {"date" : date,"predict" : test_x, "real" : test_y}
    csv = pd.DataFrame(index)
    csv.to_csv(filePath)

def changeColName(all_data):
	columns = all_data.columns
	all_data.rename(columns=lambda x: colName.get(x, x), inplace = True)
	
	print all_data.columns
	print columns
	return all_data

def preProcessData(filePath):
    all_data = pd.read_csv(filePath)
    changeColName(all_data)    
    all_data.to_csv(‘captionlocal.csv‘)    
    return all_data


all_data = preProcessData(‘caption.csv‘)   

 

修改读入的CSV文件中的列名

标签:pen   api   文件中   code   taf   ict   column   panda   change   

原文地址:https://www.cnblogs.com/yicheng-bangbang/p/8780408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!