gpt4 book ai didi

Pandas - 追加到第一列

转载 作者:行者123 更新时间:2023-12-04 08:07:52 24 4
gpt4 key购买 nike

我正在尝试向 campaign-device.csv 添加 2 个附加列( custom_conversion_idcustom_conversion_name )使用 Pandas ,但似乎没有选项将这两列附加到 .csv 的开头.有小费吗?

with open('output/campaign-device.csv', 'w', newline='') as outfile:
w = csv.DictWriter(outfile,fieldnames=items[0].keys())
w.writeheader()
w.writerows(items)

csv_input = pandas.read_csv('output/campaign-device.csv')
csv_input['custom_conversion_id'] = '11197251455862'
csv_input['custom_conversion_name'] = 'total-device'
csv_input.to_csv('output/campaign-device.csv', index=0)

最佳答案

您不能将列附加到开头,但可以在添加列后重新排列列顺序

 with open('output/campaign-device.csv', 'w', newline='') as outfile:
w = csv.DictWriter(outfile,fieldnames=items[0].keys())
w.writeheader()
w.writerows(items)

csv_input = pandas.read_csv('output/campaign-device.csv')

cols = [x for x in csv_input.columns]
# or cols = csv_input.columns.tolist()

csv_input['custom_conversion_id'] = '11197251455862'
csv_input['custom_conversion_name'] = 'total-device'

csv_input = csv_input[['custom_conversion_id', 'custom_conversion_name', *cols]]

csv_input.to_csv('output/campaign-device.csv', index=0)

关于 Pandas - 追加到第一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66135012/

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