读取csv文件pandas时给出列名 [英] Give column name when read csv file pandas
本文介绍了读取csv文件pandas时给出列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的数据集的示例.
<预><代码>>>>user1 = pd.read_csv('数据集/1.csv')>>>打印(用户 1)0 0.69464 3.1735 7.50480 0.030639 0.14982 3.48680 9.27551 0.069763 -0.29965 1.94770 9.11202 0.099823 -1.68890 1.41650 10.12003 0.129820 -2.17930 0.95342 10.92404 0.159790 -2.30180 0.23155 10.65105 0.189820 -1.41650 1.18500 11.0730如何下推第一列并在第一列添加名称列[TIME, X, Y, and Z].
想要的输出是这样的:
时间 X Y Z0 0 0.69464 3.1735 7.50481 0.030639 0.14982 3.48680 9.27552 0.069763 -0.29965 1.94770 9.11203 0.099823 -1.68890 1.41650 10.12004 0.129820 -2.17930 0.95342 10.92405 0.159790 -2.30180 0.23155 10.65106 0.189820 -1.41650 1.18500 11.0730
解决方案
我会这样做:
colnames=['TIME', 'X', 'Y', 'Z']user1 = pd.read_csv('dataset/1.csv', names=colnames, header=None)
This is the example of my dataset.
>>> user1 = pd.read_csv('dataset/1.csv')
>>> print(user1)
0 0.69464 3.1735 7.5048
0 0.030639 0.14982 3.48680 9.2755
1 0.069763 -0.29965 1.94770 9.1120
2 0.099823 -1.68890 1.41650 10.1200
3 0.129820 -2.17930 0.95342 10.9240
4 0.159790 -2.30180 0.23155 10.6510
5 0.189820 -1.41650 1.18500 11.0730
How to push down the first column and add the names column [TIME, X, Y, and Z] on the first column.
The desired output is like this:
TIME X Y Z
0 0 0.69464 3.1735 7.5048
1 0.030639 0.14982 3.48680 9.2755
2 0.069763 -0.29965 1.94770 9.1120
3 0.099823 -1.68890 1.41650 10.1200
4 0.129820 -2.17930 0.95342 10.9240
5 0.159790 -2.30180 0.23155 10.6510
6 0.189820 -1.41650 1.18500 11.0730
解决方案
I'd do it like this:
colnames=['TIME', 'X', 'Y', 'Z']
user1 = pd.read_csv('dataset/1.csv', names=colnames, header=None)
这篇关于读取csv文件pandas时给出列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文