[英]Reshaping data in CSV to multiple columns
0 19 1 19 2 19 3 19
How can i change this above csv data in python to - 我如何在python中将以上csv数据更改为-
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
...
You don't really need pandas. 您真的不需要熊猫。 You can do this with np.loadtxt
followed by a reshape
. 您可以使用np.loadtxt
进行此操作,然后进行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=',')
. 请注意,如果您使用其他定界符(例如,逗号),则可以通过传递delimiter
参数来指定它,例如: np.loadtxt(buf, delimiter=',')
。
Now, save to CSV using savetxt
- 现在,使用savetxt
保存为savetxt
np.savetxt('file.csv', arr, delimiter=',')
Later, when reading your CSV using pandas
, use - 稍后,当使用pandas
读取CSV时,请使用-
df = pd.read_csv(index_col=[0], header=None, names=['A', 'B'])
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.