我使用pandas从文本文件中创建了一个数据框。
df = pd.read_table('inputfile.txt',names=['Line'])
当我执行
df
命令时。Line
0 17/08/31 13:24:48 INFO spark.SparkContext: Run...
1 17/08/31 13:24:49 INFO spark.SecurityManager: ...
2 17/08/31 13:24:49 INFO spark.SecurityManager: ...
3 17/08/31 13:24:49 INFO spark.SecurityManager: ...
4 17/08/31 13:24:49 INFO util.Utils: Successfull...
5 17/08/31 13:24:49 INFO slf4j.Slf4jLogger: Slf4...
6 17/08/31 13:24:49 INFO Remoting: Starting remo...
7 17/08/31 13:24:50 INFO Remoting: Remoting star...
8 17/08/31 13:24:50 INFO Remoting: Remoting now ...
9 17/08/31 13:24:50 INFO util.Utils: Successfull...
现在我想将这个文件保存为csv
格式。
df.to_csv('outputfile')
我得到的结果是这样的。
0,17/08/31 13:24:48 INFO spark.SparkContext: Running Spark version 1.6.0
1,17/08/31 13:24:49 INFO spark.SecurityManager: Changing view acls to: user1
2,17/08/31 13:24:49 INFO spark.SecurityManager: Changing modify acls to: user1
3,17/08/31 13:24:49 INFO spark.SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(user1);
4,17/08/31 13:24:49 INFO util.Utils: Successfully started service 'sparkDriver' on port 17101.
5,17/08/31 13:24:49 INFO slf4j.Slf4jLogger: Slf4jLogger started
6,17/08/31 13:24:49 INFO Remoting: Starting remoting
7,17/08/31 13:24:50 INFO Remoting: Remoting started; listening on addresses :
8,17/08/31 13:24:50 INFO Remoting: Remoting now listens on addresses:
9,17/08/31 13:24:50 INFO util.Utils: Successfully started service 'sparkDriverActorSystem' on port 100033.
我希望我的输出是:
17/08/31 13:24:48 INFO spark.SparkContext: Running Spark version 1.6.0
17/08/31 13:24:49 INFO spark.SecurityManager: Changing view acls to: user1
17/08/31 13:24:49 INFO spark.SecurityManager: Changing modify acls to: user1
17/08/31 13:24:49 INFO spark.SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(user1);
17/08/31 13:24:49 INFO util.Utils: Successfully started service 'sparkDriver' on port 17101.
17/08/31 13:24:49 INFO slf4j.Slf4jLogger: Slf4jLogger started
17/08/31 13:24:49 INFO Remoting: Starting remoting
17/08/31 13:24:50 INFO Remoting: Remoting started; listening on addresses :
17/08/31 13:24:50 INFO Remoting: Remoting now listens on addresses:
17/08/31 13:24:50 INFO util.Utils: Successfully started service 'sparkDriverActorSystem' on port 100033.
我尝试了以下几种方法,但仍然得不到我想要的输出结果。
np.savetxt(r'np.txt', df.Line, fmt='%d')
df.to_csv(sep=' ', index=False, header=False)
df.columns
是什么意思? - James