0

I have several different CSV files like this:

one: 

iq, name 
69, joe
122, james
...

two:

iq_start, iq_end, category
120,500, Very Superior
120,129,    Superior
110,119,    High Average
90,109, Average
80,89,  Low Average
70,79,  Borderline
0,69,   Extremely Low

three: 
iq, career
69 , manual work
122, doctor

I want to pull the data into pandas , and output the data like this

[
"122" = {
    "name" : "james",
    "career" : "doctor"
    "category" : "Superior"
    },

"69" = {
    "name" : "joe",
    "career" : "manual work"
    "category" : "Extremely Low"
    }


]

Can panda pull this off or get me some of the way?

3
  • 3
    You could probably even do that without pandas, with the csv and json modules. With pandas should work fine as well, but what have you tried already? Commented Nov 6, 2013 at 9:23
  • I know I can do it without Pandas, but I'm wondering if I can do it in Pandas. for example can you do a join like iq between iq_start and iq_end in pandas? Commented Nov 6, 2013 at 9:59
  • Well, Pandas has a csv import, and Series and Dataframes have a to_json method, so this should definitely be possible. Commented Nov 6, 2013 at 10:49

1 Answer 1

1

Here is the code, but are you sure there are not two person with the same IQ?

import pandas as pd
import io

one="""iq, name
69, joe
120, james"""

two="""iq_start, iq_end, category
130,500, Very Superior
120,129,    Superior
110,119,    High Average
90,109, Average
80,89,  Low Average
70,79,  Borderline
0,69,   Extremely Low"""

three="""iq, career
69 , manual work
120, doctor"""

df1 = pd.read_csv(io.BytesIO(one), skipinitialspace=True)
df2 = pd.read_csv(io.BytesIO(two), skipinitialspace=True)
df3 = pd.read_csv(io.BytesIO(three), skipinitialspace=True)

iqmap = pd.Series(df2.category.values, index=df2.iq_start).sort_index()

df = pd.merge(df1, df3)
df["category"] = iqmap.asof(df.iq).values
df.set_index("iq", inplace=True)
df.T.to_dict()

output:

{69: {'career': 'manual work', 'category': 'Extremely Low', 'name': 'joe'},
 120: {'career': 'doctor', 'category': 'Superior', 'name': 'james'}}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that will get me started. The data is only a sample. The real data the "iq" is unique

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.