0

我有几个不同的 CSV 文件,如下所示:

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

我想将数据拉入 pandas ,并像这样输出数据

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

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


]

熊猫可以解决这个问题或给我一些帮助吗?

4

1 回答 1

1

这是代码,但你确定没有两个智商相同的人吗?

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()

输出:

{69: {'career': 'manual work', 'category': 'Extremely Low', 'name': 'joe'},
 120: {'career': 'doctor', 'category': 'Superior', 'name': 'james'}}
于 2013-11-06T10:49:05.937 回答