Python读取csv文件添加后缀以及新增一列
- 需求:
- 生成测试"image"csv文件;
- "image"列添加后缀;
- 添加"name"列;
#Python代码如下:
import pandas as pd
#1.生成测试文件
image = [1,2,3,4,5]
df = pd.DataFrame({'image':image})
df.to_csv("../test_data/image.csv",index=False)
#2.读取测试文件
df = pd.read_csv('../test_data/image.csv')
#3.添加后缀
df['image'] = df['image'].apply(lambda x:str(x)+'.jpg')
#4.添加1列
df['name'] = '张三'
df.to_csv('../test_data/image.csv',index=False)
- 输出结果