在pandas中从dataframe中提取值为float / int

时间:2017-07-25 21:32:42

标签: python pandas dataframe

我正在尝试根据python中数据框中的其他两列提取一列的值。但是这会返回一个序列对象。

df['A'].where((df['B'] == val1) & (df['C'] == val2))

如何在int / float中提取列A的值或如何将序列对象转换为int或float?

1 个答案:

答案 0 :(得分:0)

你可以使用这样的掩码来切片:

case 'receiveAllElements':
        const visibleElements = {};
        const unplacedElements = {};

        const elements = action.elements.reduce((result, index) => {
            result[`${index.id}`] = index;
            return result;
        }, {});

        const keys = Object.keys(elements);
        for (const key of keys) {
            const e = elements[key];

            if (e.sectorId === null) {
                unplacedElements[key] = e;
            } else {
                visibleElements[key] = e;
            }
        }

        const visibleIds = Object.keys(visibleElements);
        const unplacedIds = Object.keys(unplacedElements);
        console.log(visibleIds);
        console.log(unplacedIds); // logging these, the numbers are consistent and don't double, triple etc with each load

        return {
            ...state,
            elementsMap: {
                ...state.elementsMap,
                ...elements,
            },
            visibleElements: [...state.visibleElements, ...visibleIds],
            unplacedElements: [...state.unplacedElements, ...unplacedIds],
        };

但是如果你想要修改相应切片的值,你应该尝试使用df['A'][(df['B'] == val1) & (df['C'] == val2)] 运算符:

loc