Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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_Arrays_Pandas_Dataframe - Fatal编程技术网

Python 根据数组在列中的索引缩短数组的长度

Python 根据数组在列中的索引缩短数组的长度,python,arrays,pandas,dataframe,Python,Arrays,Pandas,Dataframe,我有一个类似这样的数据帧: signal 0 0.469112 -0.282863 -1.509059 -1.135632 1.212112 -0.173215 0.119209 -1.044236 1 0.469152 0.32863 1.509059 1.135632 1.212112 0.173215 0.419209 -1.044236 2 0.469152 0.32843 1.209059 5.

我有一个类似这样的数据帧:

    signal
0   0.469112    -0.282863 -1.509059 -1.135632   1.212112    -0.173215   0.119209  -1.044236 
1   0.469152    0.32863    1.509059  1.135632   1.212112     0.173215   0.419209  -1.044236 
2   0.469152    0.32843    1.209059  5.135632   6.212112    -0.173215   -0.419209 -7.044236 
预期产出:

 signal
0    -1.509059  -1.135632   1.212112    -0.173215   0.119209        
1    1.509059    1.135632   1.212112     0.173215   0.419209        
2    1.209059    5.135632   6.212112    -0.173215   -0.419209   

我想循环遍历“signal”(数组)列,并选择输入在每行第三和第五之间的数据。在真实数据集中,每行中的信号长度从10到8000不等。我尝试了类似df.signal.iloc[3:5]的东西,但它不起作用。那我该怎么做呢。谢谢

IIUC您需要通过
str
建立索引:

df.signal.str[2:7]
熊猫从
0
开始计数,因此对于从
3.
选择到
6.
列,请使用:

df1 = df.signal.str[2:6]
print (df1)
0    [-1.509059, -1.135632, 1.212112, -0.173215]
1       [1.509059, 1.135632, 1.212112, 0.173215]
2      [1.209059, 5.135632, 6.212112, -0.173215]
Name: signal, dtype: object

@耶斯拉勒,谢谢!我试图像这样应用于数据帧中的整个列:df.singal.apply(lambda x:x.str[300:1300]),得到了这个AttributeError:'numpy.ndarray'对象没有属性'str'@almo-为什么
应用
?使用
df.singal.str[300:1300]