Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫:在列值之间插入以返回索引名称_Python_Pandas_Python 3.6 - Fatal编程技术网

Python 熊猫:在列值之间插入以返回索引名称

Python 熊猫:在列值之间插入以返回索引名称,python,pandas,python-3.6,Python,Pandas,Python 3.6,我想在下面的代码中返回索引名和成本。我认为我的思路是正确的,但不确定如何推进这一点,我已经附加了我的代码和输出 编辑:我愿意以这种方式进行设计更改 例如a在test\u data中,它应该返回'a':'rep\u asp\u art','1000000' import pandas as pd treatments = {'min_asp_art': {'treat': 'min', 'mat': 'asp', 'class': 'art',

我想在下面的代码中返回
索引名
成本
。我认为我的思路是正确的,但不确定如何推进这一点,我已经附加了我的代码和输出

编辑:我愿意以这种方式进行设计更改

例如
a
test\u data
中,它应该返回
'a':'rep\u asp\u art','1000000'

import pandas as pd


treatments = {'min_asp_art': {'treat': 'min', 'mat': 'asp', 'class': 'art',
                              'hci': 85, 'cost': 100000},
              'maj_asp_art': {'treat': 'maj', 'mat': 'asp', 'class': 'art',
                              'hci': 60, 'cost': 350000},
              'rep_asp_art': {'treat': 'maj', 'mat': 'asp', 'class': 'art',
                              'hci': 0, 'cost': 1000000},
              'min_asp_col': {'treat': 'min', 'mat': 'asp', 'class': 'col',
                              'hci': 85, 'cost': 100000},
              'maj_asp_col': {'treat': 'maj', 'mat': 'asp', 'class': 'col',
                              'hci': 60, 'cost': 350000},
              'rep_asp_col': {'treat': 'maj', 'mat': 'asp', 'class': 'col',
                              'hci': 0, 'cost': 1000000},
              'min_chip_col': {'treat': 'min', 'mat': 'chip', 'class': 'col',
                               'hci': 70, 'cost': 30000},
              'maj_chip_col': {'treat': 'maj', 'mat': 'chip', 'class': 'col',
                               'hci': 50, 'cost': 80000},
              'rep_chip_col': {'treat': 'maj', 'mat': 'chip', 'class': 'col',
                               'hci': 0, 'cost': 100000},
              'min_chip_loc': {'treat': 'min', 'mat': 'chip', 'class': 'loc',
                               'hci': 70, 'cost': 30000},
              'maj_chip_loc': {'treat': 'maj', 'mat': 'chip', 'class': 'loc',
                               'hci': 45, 'cost': 80000},
              'rep_chip_loc': {'treat': 'maj', 'mat': 'chip', 'class': 'loc',
                               'hci': 0, 'cost': 100000},
              }

ndf = pd.DataFrame(treatments)
ndf = ndf.round(3)

dft = ndf.T


def get_treatment(material, func_class, hci):
    return dft[(dft['mat'] == material) &
               (dft['class'] == func_class) &
               (dft['hci'].astype(int) >= hci)].index


test_data = {'a': {'mat': 'asp', 'class': 'art', 'hci': 35},
             'b': {'mat': 'asp', 'class': 'art', 'hci': 90},
             'c': {'mat': 'asp', 'class': 'col', 'hci': 68},
             'd': {'mat': 'asp', 'class': 'art', 'hci': 5},
             'e': {'mat': 'chip', 'class': 'col', 'hci': 55},
             'f': {'mat': 'chip', 'class': 'loc', 'hci': 95},
             'g': {'mat': 'asp', 'class': 'art', 'hci': 15},
             'h': {'mat': 'asp', 'class': 'art', 'hci': 70},
             'i': {'mat': 'asp', 'class': 'art', 'hci': 3}
             }

for k, v in test_data.items():
    res = get_treatment(v['mat'], v['class'], v['hci'])
    print (k, res)


# Output:
# a Index(['min_asp_art', 'maj_asp_art'], dtype='object')
# b Index([], dtype='object')
# c Index(['min_asp_col'], dtype='object')
# d Index(['min_asp_art', 'maj_asp_art'], dtype='object')
# e Index(['min_chip_col'], dtype='object')
# f Index([], dtype='object')
# g Index(['min_asp_art', 'maj_asp_art'], dtype='object')
# h Index(['min_asp_art'], dtype='object')
# i Index(['min_asp_art', 'maj_asp_art'], dtype='object')
就这样

def get_treatment(material, func_class, hci):
    return dft[(dft['mat'] == material) &
               (dft['class'] == func_class) &
               (dft['hci'].astype(int) >= hci)]['cost']


# test_data = ... #

for k, v in test_data.items():
    res = get_treatment(v['mat'], v['class'], v['hci'])
    print (k, res, '\n')
输出:

> a min_asp_art    100000
> maj_asp_art    350000
> Name: cost, dtype: object 

> b Series([], Name: cost, dtype: object) 

> c min_asp_col    100000
> Name: cost, dtype: object 

> d min_asp_art    100000
> maj_asp_art    350000
> Name: cost, dtype: object 

> e min_chip_col    30000
> Name: cost, dtype: object 

> f Series([], Name: cost, dtype: object) 

> g min_asp_art    100000
> maj_asp_art    350000
> Name: cost, dtype: object 

> h min_asp_art    100000
> Name: cost, dtype: object 

> i min_asp_art    100000
> maj_asp_art    350000
> Name: cost, dtype: object 

你的预期输出是什么?@QuangHoang代表一个“代表艺术”,1000000什么是
a
?您的样本数据中没有
a
?@QuangHoang在测试数据“a”中,第一个