Add column to a specific CSV row using Python pandas -
i want merge rows in csv files matching id given dictionary.
i have dictionary: l= {2.80215: [376570], 0.79577: [378053], 22667183: [269499]}
i have csv file.
b c d 2000-01-03 -0.59885 -0.18141 -0.68828 -0.77572 2000-01-04 0.83935 0.15993 0.95911 -1.12959 2000-01-05 2.80215 -0.10858 -1.62114 -0.20170 2000-01-06 0.71670 -0.26707 1.36029 1.74254 2000-01-07 -0.45749 0.22750 0.46291 -0.58431 2000-01-10 -0.78702 0.44006 -0.36881 -0.13884 2000-01-11 0.79577 -0.09198 0.14119 0.02668 2000-01-12 -0.32297 0.62332 1.93595 0.78024 2000-01-13 1.74683 -1.57738 -0.02134 0.11596
the output should :
b c d e f 2000-01-03 -0.59885 -0.18141 -0.68828 -0.77572 0 2000-01-04 0.83935 0.15993 0.95911 -1.12959 0 2000-01-05 2.80215 -0.10858 -1.62114 -0.20170 376570 2000-01-06 0.71670 -0.26707 1.36029 1.74254 0 2000-01-07 -0.45749 0.22750 0.46291 -0.58431 0 2000-01-10 -0.78702 0.44006 -0.36881 -0.13884 0 2000-01-11 0.79577 -0.09198 0.14119 0.02668 378053 2000-01-12 -0.32297 0.62332 1.93595 0.78024 0 2000-01-13 1.74683 -1.57738 -0.02134 0.11596 0
i tried way:
import pandas data = panda.read_csv("thisfiel.csv") data["f"] = data["b"].apply(lambda x: l[x])
but couldn't aimed results.
do this:
def getval(x): try: return l[x][0] except keyerror: return 0 data['f'] = data['b'].map(getval)
Comments
Post a Comment