Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.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 将单行tsv文件转换为多行tsv文件_Python_Pandas_Csv_Dataframe - Fatal编程技术网

Python 将单行tsv文件转换为多行tsv文件

Python 将单行tsv文件转换为多行tsv文件,python,pandas,csv,dataframe,Python,Pandas,Csv,Dataframe,我有一个tsv文件,它是单行的 e、 g: onset duration stimulus 16.100000 3.000000 tasteless 26.700000 3.000000.1 control 31.700000 ... 150.6 729.900000 3.000000.60 rinse.26 745.600000 3.000000.61 112.5cal.6 751.600000 3.000000.62 rinse.27

我有一个tsv文件,它是单行的

e、 g:

onset   duration    stimulus    16.100000   3.000000    tasteless   26.700000   3.000000.1  control 31.700000   ... 150.6   729.900000  3.000000.60 rinse.26    745.600000  3.000000.61 112.5cal.6  751.600000  3.000000.62 rinse.27

0 rows × 192 columns
我打算做的是,在每三个元素之后,在下一行添加一个新行字符,这样上面的数据框应该如下所示:

onset   duration    stimulus
16.100000   3.000000    tasteless
26.700000   3.000000    control
31.700000   3.000000    rinse
48.400000   3.000000    tasteless
60.000000   3.000000    tasteless
76.600000   3.000000    tasteless
91.300000   3.000000    tasteless
103.900000  3.000000    0cal
111.900000  3.000000    rinse
127.600000  3.000000    0cal
131.600000  3.000000    rinse
150.2000
我试过了

"\n".join(["\t".join(df[i:i+3]) for i in range(0,len(df),3)])
但毫无帮助。还尝试将数据帧转换为文本,并每三次将
\t
替换为
\n


我们可以使用pandas吗?

您可以在tsv中读取数据,重塑值,然后创建新的数据帧

In [428]: df = pd.read_csv('test.tsv', header=None, delim_whitespace=True); df.values
Out[428]: 
array([['onset', 'duration', 'stimulus', 16.1, 3.0, 'tasteless', 26.7,
        '3.000000.1', 'control', 31.7, '...', 150.6, 729.9, '3.000000.60',
        'rinse.26', 745.6, '3.000000.61', '112.5cal.6', 751.6,
        '3.000000.62', 'rinse.27']], dtype=object)

In [434]: cols = df.values.reshape(-1, 3)

In [435]: df = pd.DataFrame(cols[1:], columns=cols[0]); df
Out[435]: 
   onset     duration    stimulus
0   16.1            3   tasteless
1   26.7   3.000000.1     control
2   31.7          ...       150.6
3  729.9  3.000000.60    rinse.26
4  745.6  3.000000.61  112.5cal.6
5  751.6  3.000000.62    rinse.27
在此之后,写回tsv很简单:

In [440]: df.to_csv('out.tsv', sep='\t')