【发布时间】:2018-03-11 03:56:10
【问题描述】:
让我们从我编写的极其无用的代码开始...我必须编写每一行才能将 10+1 数据集转换为 50+1 数据集
import numpy as np
import pandas as pd
import csv
import os
with open("dataset_feature_champion_number.csv","r") as source:
reader = csv.reader(source)
with open("predataset_champ_rating.csv","w",newline='') as result:
writer = csv.writer(result)
for r in reader:
writer.writerow((r[1],r[1],r[1],r[1],r[1],
r[2],r[2],r[2],r[2],r[2],
r[3],r[3],r[3],r[3],r[3],
r[4],r[4],r[4],r[4],r[4],
r[5],r[5],r[5],r[5],r[5],
r[6],r[6],r[6],r[6],r[7],
r[7],r[7],r[7],r[7],r[8],
r[8],r[8],r[8],r[8],r[9],
r[9],r[9],r[9],r[9],r[9],
r[10],r[10],r[10],r[10],r[10],r[11]))
哪个函数将 raw_dataset 转换为 pre-dataset_feature,然后我将 pre-dataset_feature 转换为“true” dataset_feature,全部在 CSV 文件中。
我的原始数据集:
blue1 blue2 blue3 blue4 blue5 red1 red2 red3 red4 red5 winner
0 125 11 59 70 124 36 129 20 135 111 0
1 23 40 77 53 95 67 73 37 132 91 0
. . . . . . . . . . . .
39501 54 112 11 27 92 7 23 87 49 66 1
预数据集:
blue1 blue1 blue1 blue1 blue1 blue2 blue2 . red5 red5 red5 red5 red5 red5 winner
125 125 125 125 125 11 11 . 111 111 111 111 111 0
23 23 23 23 23 40 40 . 91 91 91 91 91 0
我的字典:
champNum Damage Toughness Control Escape Utility
1 2 2 2 2 0
2 3 1 2 3 0
3 3 1 1 3 1
. . . . . .
125 3 2 1 1 1
. . . . . .
137 2 1 2 2 3 2
138 3 0 3 0 1 2
预计我的预数据集将变成“真实”数据集:
blue1 blue1 blue1 blue1 blue1 blue2 blue2 . red5 red5 red5 red5 red5 red5 winner
3 2 1 1 1 1 1 . 3 0 2 1 2 0
3 0 1 2 0 3 2 . 3 1 2 2 0 0
值通过手动转换 row:1,6,11,16,21,26,31,36,41,46 与“Damage”,row:2,(+5...) 直到 47 与“Thoughness "等等……
不满意,我再次打算编写稍微高效的代码...
def createDictionary2(csvfile):
with open(csvfile, mode='r') as data:
reader = csv.reader(data)
next(reader,None)
dict = {int(rows[0]):[rows[1],rows[2],rows[3],rows[4],rows[5]] for rows in reader}
return dict
def convertDataframeToAnotherFeature(csvfile,dictionary):
df = pd.read_csv(csvfile)
temp1 = df.iloc[:,1:11]
temp2 = df['winner']
temp3 = temp1.applymap(dictionary.get)
champNum = temp3.join(temp2)
return champNum
def saveAsCSV5(dataframe):
dataframe.to_csv("dataset_feature_champ_rating.csv")
def feature5():
diction = createDictionary2("champRating1.csv")
dataset = convertDataframeToAnotherFeature("dataset_feature_champion_number.csv", diction)
saveAsCSV5(dataset)
feature5()
结果是这样的:
blue1 blue2 blue3 blue4 blue5 red1 red2 red3 red4 red5 winner
0 ['3', '2', '1', '1', '1'] ['1', '1', '3', '2', '3'] ['3', '0', '1', '0', '1'] ['3', '1', '0', '2', '0'] ['3', '1', '2', '0', '0'] ['3', '2', '1', '2', '2'] ['3', '2', '3', '1', '0'] ['3', '2', '2', '0', '0'] ['3', '0', '3', '1', '0'] ['1', '2', '3', '1', '3'] 0
1 ['3', '0', '1', '2', '0'] ['3', '2', '2', '2', '0'] ['3', '1', '0', '3', '2'] ['3', '1', '1', '1', '3'] ['3', '1', '2', '2', '2'] ['1', '3', '3', '1', '0'] ['2', '1', '3', '0', '2'] ['2', '2', '2', '2', '0'] ['3', '1', '2', '3', '2'] ['3', '2', '2', '3', '0'] 0
我认为它稍微好一点,但它让我更加困惑,因为我不知道如何扩展每列中的那些“列表”以扩展并填充它旁边的 4 列...
我对描述和解决问题的精确“术语”缺乏了解,因此我无法通过浏览、观看免费在线课程或阅读文档来有效地搜索解决问题的正确方法。
编辑:包括以前没有的“ConvertDataframeToAnotherFeature”函数
【问题讨论】:
-
请编辑问题以包含缺少的功能
convertDataframeToAnotherFeature() -
哦,对了,对不起。它在笔记本的另一行,忘记复制了。
标签: python pandas csv dictionary dataset