创建随机 DataFrame 并写入 .csv

创建一个简单的 DataFrame。

import numpy as np
import pandas as pd

# Set the seed so that the numbers can be reproduced.
np.random.seed(0)  

df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))

# Another way to set column names is "columns=['column_1_name','column_2_name','column_3_name']"

df

      A         B         C
0  1.764052  0.400157  0.978738
1  2.240893  1.867558 -0.977278
2  0.950088 -0.151357 -0.103219
3  0.410599  0.144044  1.454274
4  0.761038  0.121675  0.443863

现在,写入 CSV 文件:

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

example.csv 的内容:

A,B,C
1.76405234597,0.400157208367,0.978737984106
2.2408931992,1.86755799015,-0.977277879876
0.950088417526,-0.151357208298,-0.103218851794
0.410598501938,0.144043571161,1.45427350696
0.761037725147,0.121675016493,0.443863232745

请注意,我们指定 index=False,以便自动生成的索引(行#s 0,1,2,3,4)不包含在 CSV 文件中。如果需要索引列,请包括它,如下所示:

df.to_csv('example.csv', index=True)  # Or just leave off the index param; default is True

example.csv 的内容:

,A,B,C
0,1.76405234597,0.400157208367,0.978737984106
1,2.2408931992,1.86755799015,-0.977277879876
2,0.950088417526,-0.151357208298,-0.103218851794
3,0.410598501938,0.144043571161,1.45427350696
4,0.761037725147,0.121675016493,0.443863232745

另请注意,如果 header=False 不需要,你可以删除标题。这是最简单的输出:

df.to_csv('example.csv', index=False, header=False)

example.csv 的内容:

1.76405234597,0.400157208367,0.978737984106
2.2408931992,1.86755799015,-0.977277879876
0.950088417526,-0.151357208298,-0.103218851794
0.410598501938,0.144043571161,1.45427350696
0.761037725147,0.121675016493,0.443863232745

分隔符可以通过 sep= 参数设置,尽管 csv 文件的标准分隔符是','

df.to_csv('example.csv', index=False, header=False, sep='\t')

1.76405234597    0.400157208367    0.978737984106
2.2408931992    1.86755799015    -0.977277879876
0.950088417526    -0.151357208298    -0.103218851794
0.410598501938    0.144043571161    1.45427350696
0.761037725147    0.121675016493    0.443863232745