Question: Suppose I have two csv files
file1.csv

```
event_id, polarity
   1124,   0.3763
  36794,   0.638
 dhejjd,   0.3627

```

file2.csv

```
event_id, tallies
   61824,   0.3
   36794,   0.8
   dhejjd,   0.9
   dthdnb,   0.66

```

I want to access the polarity and tallies for each event\_id. how can i read these files in 2 arrays so that for each [event\_id] i can get the polarity and tallies and then perform my calculations with these two values.
i was trying this but didnt work out I got an error:

```
 for event_id, polarity in file1reader: ValueError: need more than 1 value to unpack

```

My code:
 import csv

```
file1reader = csv.reader(open("file1.csv"), delimiter=",")
file2reader = csv.reader(open("file2.csv"), delimiter=",")

header1 = file1reader.next() #header
header2 = file2reader.next() #header

for event_id, polarity in file1reader:
    #if event_id and polarity in file1reader:
      for event_id, tallies in file2reader:
        #if event_id in file2reader:
          if file1reader.event_id == file2reader.event_id:
            print event_id, polarity, tallies   
            break   
file1reader.close()
file2reader.close() 

```

Answer: 
Use [pandas](http://pandas.pydata.org/) data frames instead of numpy arrays

```
import pandas as pd
df = pd.read_csv("file1.csv", index_col="event_id", skipinitialspace=True)
df2 = pd.read_csv("file2.csv", index_col="event_id", skipinitialspace=True)
df = df.merge(df2, how='outer', left_index=True, right_index=True)

```

P.S. Corrected the code so that it runs. The 'outer' join means that if only 'polarity' or 'tallies' exist for a given 'event\_id', then missing values are coded as `NaN`s. The output is 

```
          polarity  tallies
event_id                   
1124        0.3763      NaN
36794       0.6380     0.80
61824          NaN     0.30
dhejjd      0.3627     0.90
dthdnb         NaN     0.66

```

If you need only rows where both are present, use `how='inner'` 

P.P.S
To work with this data frame further you can, for example,
replace `NaNs` with some value, let us say `0`:

```
df.fillna(0, inplace=True)

```

You can select elements by label

```
df.loc["dhejjd","polarity"]
df.loc[:,"tallies"]

```

or by integer position 

```
df.iloc[0:3,:]

```

If you never used pandas, it takes some time to learn it and get used to it. And it is worth every second.
You don't need to loop over both `csvreader` objects. you can first use `itertools.chain` to concatenate 2 `csvreader`. Then use a dictionary (with [setdefault method](https://docs.python.org/2/library/stdtypes.html#dict.setdefault) )to store the `event_id`'s as the keys and `polarity` as the values.

```
import csv
from itertools import chain
d={}
with open('a1.txt', 'rb') as csvfile1,open('ex.txt', 'rb') as csvfile2:
     spamreader1 = csv.reader(csvfile1, delimiter=',')
     spamreader2 = csv.reader(csvfile2, delimiter=',')
     spamreader1.next()
     spamreader2.next()
     sp=chain(spamreader1,spamreader2)
     for i,j in sp:
            d.setdefault(i,[]).append(j)
     print d

```

result :

```
{'36794': ['0.638', '0.8'], 
 '61824': ['0.3'], 
 '1124': ['0.3763'], 
 'dthdnb': ['0.66'], 
 'dhejjd': ['0.3627', '0.9']}

```