将数据框保存为pandas中的csv / text文件而不进行行编号

时间:2017-09-06 17:30:26

标签: python pandas

我在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)

3 个答案:

答案 0 :(得分:1)

看起来这个数字可能是Line列中字符串的一部分。您可以使用任何内容替换前导数字和空格,并使用以下内容将其输出到没有索引的文件:

df.Line.str.replace('^\d+ +','').to_csv('outputfile.csv', index=False, header=False)

答案 1 :(得分:1)

鉴于特殊情况,

詹姆斯的答案很可能是正确的。 但是,df_injury_record = pd.DataFrame({'Surface':list('aaaabbbbddd'), 'BodyPart':list('abbbbdaaadd')}) Frequency_BodyPart = df_injury_record.groupby(["Surface","BodyPart"]).size() print (Frequency_BodyPart) Surface BodyPart a a 1 b 3 b a 2 b 1 d 1 d a 1 d 2 dtype: int64 Frequency_BodyPart = Frequency_BodyPart.rename_axis(index={'Surface': 'Class'}) print (Frequency_BodyPart) Class BodyPart a a 1 b 3 b a 2 b 1 d 1 d a 1 d 2 dtype: int64 的标准行为是将行号放在前面没有标题的列中。 要删除此选项,只需将df = Frequency_BodyPart.reset_index(name='count').rename(columns={'Surface': 'Class'}) print (df) Class BodyPart count 0 a a 1 1 a b 3 2 b a 2 3 b b 1 4 b d 1 5 d a 1 6 d d 2 参数设置为pandas

index=

答案 2 :(得分:0)

克里斯蒂安几乎是对的。如果您查看 to_csv command 的文档。

根据文档

index : boolean, default True, Write row names (index)

我强烈推荐使用辅助工具 Kite 来帮助处理诸如此类的简单事情。

df.to_csv('outfile.csv', index=False)