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

在python中准备数据,一列按字符分隔成两列

在python中准备数据,一列按字符分隔成两列,python,pandas,signals,data-conversion,Python,Pandas,Signals,Data Conversion,我有一个从spike2导出为.txt的文件,其中包含两个长度相同的信号。我使用pandas.read\u cvs导入文件。 该文件由19行字符组成,然后在一列中开始我的信号值。在中间有两行字符,并开始我的第二信号的值。与此模式类似: "text'.........." "text'.........." ... ... "text'.........." "text'.........." 1.5 2.71 ... ... ... 0.56 "text'.........." 1.98 0.56

我有一个从
spike2
导出为
.txt
的文件,其中包含两个长度相同的信号。我使用
pandas.read\u cvs
导入文件。 该文件由19行字符组成,然后在一列中开始我的信号值。在中间有两行字符,并开始我的第二信号的值。与此模式类似:

"text'.........."
"text'.........."
...
...
"text'.........."
"text'.........."
1.5
2.71
...
...
...
0.56
"text'.........."
1.98
0.567
...
...
...
6.89
我想自动分离我的两个信号,一个接一个地绘制它们(共享x轴),并绘制每个信号的光谱图

但直到现在,我还不能轻易地把我的两个信号分开。

您可以通过以下几个步骤完成此操作:

  • 使用的和参数可以在读取文件时忽略前几行

  • 使用和删除所有文本行

  • 向下拆分一半,然后放入另一列中,后跟
    len(df)/2

  • 假设您有,只需调用display即可

  • 例如:

    %matplotlib inline
    import pandas as pd
    from cStringIO import StringIO
    
    text_file = '''text line
    text line
    text line
    text line
    text line
    text line
    1.5
    2.71
    0.567
    2.71
    2.71
    0.56
    text line
    1.98
    0.567
    1.98
    2.71
    0.56
    6.89'''
    
    # Read in data with, separate data with newline (\n) and skip the first n lines
    # StringIO(text_file) is for example only
    # Normally, you would use pd.read_csv('/path/to/file.csv', ...)
    df = pd.read_csv(StringIO(text_file), sep='\n', header=None, skiprows=6)
    print 'Two signals:'
    print df
    print
    
    print 'Force to numbers:'
    df = df.apply(pd.to_numeric, errors='coerce')
    print df
    print
    
    print 'Remove NaNs:'
    df = df.dropna().reset_index().drop('index', 1)
    print df
    print
    
    # You should have 2 equal length signals, one after the other, so split half way
    print 'Split into two columns:'
    s1 = df[:len(df)/2].reset_index().drop('index', 1)
    s2 = df[len(df)/2:].reset_index().drop('index', 1)
    df = pd.concat([s1, s2], axis=1)
    df.columns = ['sig1', 'sig2']
    print df
    print
    
    # Plot, assuming you have matplotlib library
    df.plot()
    



    光谱图将不得不等待…

    你真的想以专横的方式将它们分开吗?你是说自动?我只是不想手动计算第一个信号的列中有多少行…是的,对不起,我没有看到它…我无法抗拒,它看起来太有趣了,如果代码给你带来麻烦,你可能会觉得这样。哈哈。我知道我以前在代码中也有过这样的感觉:)看看下面我的答案,给你一个开始。这是一个完美的例子!令人惊叹的!光谱图几乎应该是另一个问题。现在您已经有了可计算形式的数据,您应该能够在SO或google上找到其他光谱图示例。
    Two signals:
                0
    0         1.5
    1        2.71
    2       0.567
    3        2.71
    4        2.71
    5        0.56
    6   text line
    7        1.98
    8       0.567
    9        1.98
    10       2.71
    11       0.56
    12       6.89
    
    Force to numbers:
            0
    0   1.500
    1   2.710
    2   0.567
    3   2.710
    4   2.710
    5   0.560
    6     NaN
    7   1.980
    8   0.567
    9   1.980
    10  2.710
    11  0.560
    12  6.890
    
    Remove NaNs:
            0
    0   1.500
    1   2.710
    2   0.567
    3   2.710
    4   2.710
    5   0.560
    6   1.980
    7   0.567
    8   1.980
    9   2.710
    10  0.560
    11  6.890
    
    Split into two columns:
        sig1   sig2
    0  1.500  1.980
    1  2.710  0.567
    2  0.567  1.980
    3  2.710  2.710
    4  2.710  0.560
    5  0.560  6.890