将熊猫子系列设置为列表

时间:2019-05-23 20:07:42

标签: python pandas

我有一个像这样的熊猫系列:

(all, timestamp, value)    NaN
(1, Ax, value)             NaN
(1, Ay, value)             NaN
...
(1, gyro_enabled, value)   NaN
(2, gyro_enabled, value)   NaN
(3, gyro_enabled, value)   NaN
(4, gyro_enabled, value)   NaN
(5, gyro_enabled, value)   NaN
(6, gyro_enabled, value)   NaN

我有以下列表:

gyr_en = [0, 1, 1, 1, 1, 1]

我想将每个(i, gyro_enabled, value)设置为gyr_en中的相应值。

除了循环遍历gyr_en并分别设置每个值外,我不知道该怎么做。还有更多类似熊猫的方法吗?

与此相反:

        for i in range(0, 6):
            idx = i
            imu_name = i+1
            new_row[imu_name, 'gyro_enabled', 'value'] = gyr_en[i]

产生以下内容:

(all, p-count, value)       NaN
(all, timestamp, value)     NaN
(1, Ax, value)              NaN
(1, Ay, value)              NaN
(1, Az, value)              NaN
(1, Gx, value)              NaN
(1, Gy, value)              NaN
(1, Gz, value)              NaN
(2, Ax, value)              NaN
...
(6, Gx, value)              NaN
(6, Gy, value)              NaN
(6, Gz, value)              NaN
(6, T, value)               NaN
(integrated, Ax, value)     NaN
(integrated, Ay, value)     NaN
(integrated, Az, value)     NaN
(integrated, Gx, value)     NaN
(integrated, Gy, value)     NaN
(integrated, Gz, value)     NaN
(integrated, T, value)      NaN
(1, gyro_enabled, value)    0.0
(2, gyro_enabled, value)    1.0
(3, gyro_enabled, value)    1.0
(4, gyro_enabled, value)    1.0
(5, gyro_enabled, value)    1.0
(6, gyro_enabled, value)    1.0
dtype: float64

1 个答案:

答案 0 :(得分:0)

假设您的pandas.Series被命名为s

from operator import itemgetter

s.loc[s.index.map(itemgetter(1)) == 'gyro_enabled'] = gyr_en