我有几个不同的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"
}
]
熊猫可以把它拉下来或让我走一条路吗?
答案 0 :(得分: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'}}