在合并的csv文件中添加其他列 [英] Add additional column in merged csv file
问题描述
我的代码合并了csv文件,并用熊猫删除了重复项.是否可以在单个合并文件中添加带有值的附加标题?
My code merges csv files and removes duplicates with pandas. Is it possible to add an additional header with values to the single merged file?
附加标头应称为 Host Alias
,并且应与 Host Name
The additional header should be called Host Alias
and should correspond to Host Name
例如主机名
是 dpc01n1
,相应的 Host Alias
应该是 dev_dom1
主机名
是 dpc02n1
,相应的 Host Alias
应该是 dev_dom2
等
E.g. Host Name
is dpc01n1
and the corresponding Host Alias
should be dev_dom1
Host Name
is dpc02n1
and the corresponding Host Alias
should be dev_dom2
etc.
这是我的代码
from glob import glob
import pandas as pd
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
input_path = r'C:\Users\urale\Desktop\logs'
output_path = r'C:\Users\urale\Desktop\logs' + '\\'
output_name = 'output.csv'
stock_files = sorted(glob(input_path + '\pc_dblatmonstat_*_*.log'))
print(bcolors.OKBLUE + 'Getting .log files from', input_path)
final_headers = [
'Start Time',
'epoch',
'Host Name',
'Db Alias',
'Database',
'Db Host',
'Db Host IP',
'IP Port',
'Latency (us)'
]
#read in files via list comprehension
content = [pd.read_csv(f,usecols = final_headers, sep='[;]',engine='python')
for f in stock_files]
print(bcolors.OKBLUE + 'Reading files')
#combine files into one dataframe
combo = pd.concat(content,ignore_index = True)
print(bcolors.OKBLUE + 'Combining files')
#drop duplicates
combo = combo.drop_duplicates()
#combo = combo.drop_duplicates(final_headers, keep=False)
print(bcolors.OKBLUE + 'Dropping duplicates')
#write to csv:
combo.to_csv(output_path + output_name, index = False)
print(bcolors.OKGREEN + 'Merged file output to', output_path, 'as', output_name)
推荐答案
def func(row):
if row['Host Name'] == "dpc01n1":
return 'dev_dom1'
#do your Host Alias generate logic here,and return
combo["Host Alias"]=combo.apply(func, axis=1)
DataFrame.apply接受一个函数来生成新的Series或DataFrame
DataFrame.apply accept a function to generate a new Series or DataFrame
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html
这篇关于在合并的csv文件中添加其他列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!