首页 > 解决方案 > 通过python在csv中设置“类型相同”

问题描述

我是python的初学者。我有文件结构 CSV

制作 模型 文本
1997 讴歌 分类 A B C D
1998 讴歌 分类 A B C D
1999 艾丽 分类 A B C D
2006年 奥迪 A7 axyz
2007年 奥迪 A8 axyz
2008年 奥迪 A6 axyz
2017 别克 卡萨达 zzxxyy
2007年 别克 卡萨达 zzxxyy
2018 别克 卡萨达 zzxxyy
... ... ... ...

我将更改为 new_csv 文件:(读取输入 csv 文件,并写入 new_csv 文件)

制作 模型 文本 类型
1997 讴歌 分类 A B C D type_0001
1998 讴歌 分类 A B C D type_0001
1999 艾丽 分类 A B C D type_0001
2006年 奥迪 A7 axyz type_0002
2007年 奥迪 A8 axyz type_0002
2008年 奥迪 A6 axyz type_0002
2017 别克 卡萨达 zzxxyy type_0003
2007年 别克 卡萨达 zzxxyy type_0003
2018 别克 卡萨达 zzxxyy type_0003
... ... ... ... ...

我已删除重复项并设置“类型”。和 VLOOKUP 'type' by 'text'。但是“文本”列有 len() > 2000 个字符。Excel VLOOKUP 与“类型”不匹配。

请帮助用python编写TUT代码。太感谢了。

我使用 csv lib:我新 python 7 天。

import csv

with open('link1.csv', newline='') as csvfile:
    #reader = csv.reader(csvfile)
    reader = csv.reader(csvfile, delimiter = ',', skipinitialspace = True)
    array = []
    for row in reader:
        print (row)
        print ('------------')
        print (row[3])
        print ('++++++++++++++')
        #????
        
header = ['year','make','model','text','type']  
with open("data.csv", 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(header)
    csvwriter.writerows(array)

标签: pythonexcelcsv

解决方案


对于这个问题,我会使用 Pandas 而不是 csv 库。

看看这样的东西。

# Importing the libraries.
import pandas as pd

# Creating a Pandas DataFrame.
df = pd.DataFrame([{'year': 1997, 'make': 'Acura', 'model': 'cl', 'text': 'abcd'},
               {'year': 1998, 'make': 'Acura', 'model': 'cl', 'text': 'abcd'},
               {'year': 1999, 'make': 'Elle', 'model': 'cl', 'text': 'abcd'},
               {'year': 2006, 'make': 'Audi', 'model': 'A7', 'text': 'abxyz'},
               {'year': 2007, 'make': 'Audi', 'model': 'A8', 'text': 'abxyz'},
               {'year': 2008, 'make': 'Audi', 'model': 'A6', 'text': 'abxyz'},
               {'year': 2017, 'make': 'buick', 'model': 'casada', 'text': 'zzxxyy'},
               {'year': 2017, 'make': 'buick', 'model': 'casada', 'text': 'zzxxyy'},
               {'year': 2018, 'make': 'buick', 'model': 'casada', 'text': 'zzxxyy'}])
# Printing out our dataset.
print(df)
# Getting unique `text` values.
list_unique_text = list(df['text'].unique())
# Creating a dictionary to map the `text` values to `type` values.
dict_text_to_type_mapper = {text: 'type_{0:0>4}'.format(index + 1) for index, text in enumerate(list_unique_text)}
# Let's see what our mapper looks like.
print(dict_text_to_type_mapper)
# Let's now create a new column in our data and fill it with values.
df['type'] = df.apply(lambda row: dict_text_to_type_mapper[row['text']], axis=1)
# Here's our new DataFrame.
print(df)

在您的情况下,您需要阅读 CSV 文件,而不是手动创建 DataFrame。

df = pd.read_csv(PATH_TO_YOUR_CSV)

使用上面的代码,我已经转换了这些数据

    make   model    text  year
0  Acura      cl    abcd  1997
1  Acura      cl    abcd  1998
2   Elle      cl    abcd  1999
3   Audi      A7   abxyz  2006
4   Audi      A8   abxyz  2007
5   Audi      A6   abxyz  2008
6  buick  casada  zzxxyy  2017
7  buick  casada  zzxxyy  2017
8  buick  casada  zzxxyy  2018

进入这个

    make   model    text  year       type
0  Acura      cl    abcd  1997  type_0001
1  Acura      cl    abcd  1998  type_0001
2   Elle      cl    abcd  1999  type_0001
3   Audi      A7   abxyz  2006  type_0002
4   Audi      A8   abxyz  2007  type_0002
5   Audi      A6   abxyz  2008  type_0002
6  buick  casada  zzxxyy  2017  type_0003
7  buick  casada  zzxxyy  2017  type_0003
8  buick  casada  zzxxyy  2018  type_0003

推荐阅读