Python 将CSV中的数据重塑为多列

Python 将CSV中的数据重塑为多列,python,pandas,csv,reshape,Python,Pandas,Csv,Reshape,如何将python中的上述csv数据更改为- 0 19 1 19 2 19 3 19 现在我需要帮助重塑我的数据集,如下所示- 0 19 1 19 2 19 3 19 我想以以下格式重新塑造我的数据集- 0 100 1 100 2 100 3 100 4 100 5 100 6 200 7 200 8 200 9 200 0 200 1 200 .....

如何将python中的上述csv数据更改为-

0   19   1   19   2   19  3   19
现在我需要帮助重塑我的数据集,如下所示-

0   19
1   19
2   19
3   19
我想以以下格式重新塑造我的数据集-

0   100   1   100   2   100  3   100  4   100  5   100     
6   200   7   200   8   200  9   200  0   200  1   200  
.....    

你真的不需要熊猫。您可以使用
np.loadtxt
重塑来完成此操作

0 100
1 100
2 100
3 100
4 100
5 100
..
6 200
7 200
8 200
9 200
0 200
1 200
...
请注意,如果您有一个不同的分隔符(例如逗号),那么可以通过如下方式传递
分隔符
参数来指定它:
np.loadtxt(buf,delimiter=',')

现在,使用
savetxt
-

import io

# replace this with your filename 
buf = io.StringIO('''0   19   1   19   2   19  3   19''')   # buf = 'file.txt'

arr = np.loadtxt(buf).reshape(-1, 2)    
arr

array([[  0.,  19.],
       [  1.,  19.],
       [  2.,  19.],
       [  3.,  19.]])
稍后,使用
pandas
读取CSV时,请使用-

np.savetxt('file.csv', arr, delimiter=',')

你真的不需要熊猫。您可以使用
np.loadtxt
重塑来完成此操作

0 100
1 100
2 100
3 100
4 100
5 100
..
6 200
7 200
8 200
9 200
0 200
1 200
...
请注意,如果您有一个不同的分隔符(例如逗号),那么可以通过如下方式传递
分隔符
参数来指定它:
np.loadtxt(buf,delimiter=',')

现在,使用
savetxt
-

import io

# replace this with your filename 
buf = io.StringIO('''0   19   1   19   2   19  3   19''')   # buf = 'file.txt'

arr = np.loadtxt(buf).reshape(-1, 2)    
arr

array([[  0.,  19.],
       [  1.,  19.],
       [  2.,  19.],
       [  3.,  19.]])
稍后,使用
pandas
读取CSV时,请使用-

np.savetxt('file.csv', arr, delimiter=',')

对于多行,如何应用此选项?0 19 1 19 2 19 3 19 4 19 5 19 6 19 7 19@Saf如果有其他查询,请编辑您的问题,或询问新问题。对于多行,如何应用此查询?0 19 1 19 2 19 3 19 4 19 5 19 6 19 7 19@Saf如果您有其他问题,请编辑您的问题,或提出新问题。