Given that the question you posed doesn’t have enough information, I’ve formulated an answer that implements the rules you wrote about. Unfortunately, it’s hard to validate this because the expected output you provided doesn’t follow the rules (e.g. 133344444,3,106029,106961,12981,3_1category). I’ve also made the assumption that if an NA value is found, it should be treated as a zero integer value.
def compute_from_row(row):
# Replace NA with value of 0
row = ['0' if 'NA' in x else x for x in row]
# Convert to integers ignoring the first column
row[1:] = map(int, row[1:])
numbers = row[2:5]
# Implement the rules
if row[1] == 3 and all(map(lambda x: x<=3000, numbers)):
return '3_category'
elif row[1] == 3 and all(map(lambda x: x<15000 and x>10000, numbers)):
return '3_1category'
elif row[1] == 2 and all(map(lambda x: x<=3000, numbers)):
return '2_category'
elif row[1] == 2 and all(map(lambda x: x<15000 and x>10000, numbers)):
return '2_1category'
elif row[1] == 1:
return '1_category'
import csv
with open('1.csv') as k1, open('out.csv', 'w') as k2:
reader = csv.reader(k1)
writer = csv.writer(k2)
headers = next(reader)
headers.append('new_column')
writer.writerow(headers)
for row in reader:
new_value = compute_from_row(row)
row.append(new_value)
writer.writerow(row)
2
solved python 2.7 script to add a column