gpt4 book ai didi

python - 在合并的 csv 文件中添加附加列

转载 作者:行者123 更新时间:2023-12-04 10:13:47 24 4
gpt4 key购买 nike

我的代码合并了 csv 文件并使用 Pandas 删除了重复项。
是否可以向单个合并文件添加带有值的附加标题?

附加 header 应称为 Host Alias并且应该对应于 Host Name
例如。 Host Namedpc01n1和相应的 Host Alias应该是 dev_dom1 Host Namedpc02n1和相应的 Host Alias应该是 dev_dom2等等。

这是我的代码

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

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html

关于python - 在合并的 csv 文件中添加附加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61180155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com