Python 使用数值参数值保存csv文件
我有一些参数Python 使用数值参数值保存csv文件,python,Python,我有一些参数 p=221 q=4.22 r=3.3 我想将输出值保存在名为p_221_uq_4.22_ur_3.3.csv的文件中 我试过这个 file_name = open('p_{}__q_{}__r_{}.csv'.format(p,q,r),'w') output.to_csv(filename+'.csv', index=False) 但是错误是“替换索引2超出了位置参数元组的范围”如果输出变量是一个数据帧,只需使用to_csv()函数: p=221 q=4.22 r=3.3 o
p=221
q=4.22
r=3.3
我想将输出值保存在名为p_221_uq_4.22_ur_3.3.csv的文件中
我试过这个
file_name = open('p_{}__q_{}__r_{}.csv'.format(p,q,r),'w')
output.to_csv(filename+'.csv', index=False)
但是错误是“替换索引2超出了位置参数元组的范围”如果
输出
变量是一个数据帧,只需使用to_csv()
函数:
p=221
q=4.22
r=3.3
output = pd.DataFrame([p,q,r])
file_name = 'p_{}__q_{}__r_{}.csv'.format(p,q,r)
output.to_csv(file_name)
使用to_csv()函数可以避免使用open()打开任何文件。输出变量的格式是什么?是熊猫数据帧吗?是,输出是熊猫数据帧