Reputation: 2103
I am using the following to write to a csv file with python:
submission = pandas.DataFrame({
"label": data["label"],
predictors[0]: data[predictors[0]]
})
and later I write a csv file with:
submission.to_csv("test.csv", index=False)
Now, my predictor list is a long list of 1000+ elements, and obviously I cannot write all those lines by hand
submission = pandas.DataFrame({
"label": data["label"],
predictors[0]: data[predictors[0]]
predictors[1]: data[predictors[1]]
predictors[2]: data[predictors[2]]
predictors[3]: data[predictors[3]]
...
})
I tried
submission = pandas.DataFrame({
"label": data["label"],
for i in range(lengthofpredictors):
predictors[i]: data[predictors[i]]
})
but it does not work, as
submission = pandas.DataFrame({
"label": data["label"],
predictors: data[predictors]
})
won't work. How do I do it?
Upvotes: 0
Views: 107
Reputation: 76297
How about using list comprehension to create the dictionary?
tmp = dict([("label", data["label"])] + \
[(predictors[i], data[predictors[i]]) for i in range(784)])
Then
submission = pandas.DataFrame(tmp)
Upvotes: 1