Reshaping data in CSV to multiple columns

北慕城南 提交于 2019-12-11 16:57:56

问题


0   19   1   19   2   19  3   19

How can i change this above csv data in python to -

0   19
1   19
2   19
3   19

now i need help with reshaping my dataset which looks like this -

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

I want to reshape my dataset in the following format -

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

回答1:


from io import StringIO

txt = """0   19   1   19   2   19  3   19
"""

df = pd.read_csv(StringIO(txt),header=None,sep=' ')
df=df.dropna(1)

pd.DataFrame(df.T[0].values.reshape(df.shape[1]//2,2))
Out[77]: 
   0   1
0  0  19
1  1  19
2  2  19
3  3  19



回答2:


You don't really need pandas. You can do this with np.loadtxt followed by a reshape.

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.]])

Note that if you have a different delimiter (example, comma), then you can specify it by passing a delimiter argument like so: np.loadtxt(buf, delimiter=',').

Now, save to CSV using savetxt -

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

Later, when reading your CSV using pandas, use -

df = pd.read_csv(index_col=[0], header=None, names=['A', 'B'])


来源:https://stackoverflow.com/questions/48179906/reshaping-data-in-csv-to-multiple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!