如何为每隔一行的数据帧的浮点索引添加值?

agu*_*zul 6 python numpy pandas

我正在以2000 Hz的频率记录数据,这意味着每0.5毫秒我就有另一个数据点。但是我的记录软件只能以1毫秒的精度记录,因此这意味着我在使用float类型的数据帧索引中有重复的值。

因此,为了修复重复项,我想向索引的其他每一行添加0.005。我尝试了这个,但是到目前为止它不起作用:

c = df.iloc[:,0] # select the first column of the dataframe
c = c.iloc[::-1]  # reverse order so that time is increasing not decreasing
pd.set_option('float_format', '{:f}'.format) # change the print output to show the decimals (instead of 15.55567E9)
i = c.index # get the index of c - the length is 20000
rp = np.matlib.repmat([0, 0.0005], 1, 10000) # create an array to repeat .0005 0 so that we can add 0.005 to every other row
df.set_index(c, i+rp).astype(float).applymap('{:,.4f}'.format) # set the index of c to i+rp - attempt to format to 4 decimals
print(c) # see if it worked
Run Code Online (Sandbox Code Playgroud)

预期输出:(已修剪以节省空间-不显示所有20,000行)

1555677243.401000   4.569000
1555677243.401500   4.569000
1555677243.402000   4.571000
1555677243.402500   4.574000
1555677243.403000   4.574000
1555677243.403500   4.576000
1555677243.404000   4.577000
1555677243.404500   4.577000
1555677243.405000   4.577000
1555677243.405500   4.581000
1555677243.406000   4.581000
1555677243.406500   4.582000
1555677243.407000   4.581000
1555677243.407500   4.582000
1555677243.408000   4.580000
1555677243.408500   4.580000
1555677243.409000   4.582000
1555677243.409500   4.585000
1555677243.410000   4.585000
1555677243.410500   4.585000
Run Code Online (Sandbox Code Playgroud)

实际输出:(注意索引中的重复项)

1555677243.401000   4.569000
1555677243.401000   4.569000
1555677243.402000   4.571000
1555677243.402000   4.574000
1555677243.403000   4.574000
1555677243.403000   4.576000
1555677243.404000   4.577000
1555677243.404000   4.577000
1555677243.405000   4.577000
1555677243.405000   4.581000
1555677243.406000   4.581000
1555677243.406000   4.582000
1555677243.407000   4.581000
1555677243.407000   4.582000
1555677243.408000   4.580000
1555677243.408000   4.580000
1555677243.409000   4.582000
1555677243.409000   4.585000
1555677243.410000   4.585000
1555677243.410000   4.585000
Run Code Online (Sandbox Code Playgroud)

WeN*_*Ben 1

IIUC 数据来自 gmds

df.index+=np.arange(len(df))%2*0.0005
df
        0
0.0000  0
0.0015  1
0.0020  2
0.0035  3
0.0040  4
0.0055  5
0.0060  6
0.0075  7
0.0080  8
0.0095  9
Run Code Online (Sandbox Code Playgroud)