I have a csv file in the with three columns so that, each row is in the format:
"abcdef" "uvwxyz" 0
I want to generate a list of tuples, where the first element of the tuple is a dictionary of some features extracted from the first two columns, while the second element is simply the third column (0 or 1 value) values representing the label of the features.
I tried the following but it throws some syntax error saying i is undefined in the last line:
dataframe = pd.read_csv(csv_file, header = None, delimiter = "\t")
a = dataframe[0]
b = dataframe[1]
label = dataframe[2]
feature = [(findFeature(x,y), labels) for x,y in i for i, labels in zip(zip(a,b), label)]
Where am I wrong?
看来您需要:
feature = [(findFeature(x,y), label) for x,y, label in zip(a,b,label)]
if you don't need any further transformations you may use csv
library instead of pandas
:
import csv
with open(csv_file) as f:
reader = csv.reader(f)
feature = [(findFeature(x,y), z) for x,y,z in reader]
you can find and example for csv
package usage here
I'm guessing you need to transform this ("abcdef", "uvwxyz", 0)
into ("abcdef", 0, "uvwxyz")
:
with open(csv_file, "r") as f:
dataframe = [(a,c,b) for a,b,c in map(lambda x: x.split("\t"), f)]
unpacking the tuple a,b,c
in when splitting each line and repacking to (a,c,b)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.