【问题标题】:Python: Printing dataframe to csvPython:将数据帧打印到 csv
【发布时间】:2015-12-21 23:05:12
【问题描述】:

我目前正在使用此代码:

import pandas as pd
AllDays = ['a','b','c','d']
TempDay = pd.DataFrame( np.random.randn(4,2) ) 
TempDay['Dates'] = AllDays
TempDay.to_csv('H:\MyFile.csv', index = False, header = False)

但是当它打印时,它会在带有标题行的日期之前打印数组。我正在寻求打印 TemperatureArray 之前的日期并且没有标题行。

编辑: 该文件带有 TemperatureArray 后跟日期:[ TemperatureArray, Date]。

-0.27724356949570034,-0.3096554106726788,a
-0.10619546908708237,0.07430127684522048,b
-0.07619665345406437,0.8474460146082116,c
0.19668718143436803,-0.8072994364484335,d

我要打印:[ Date TemperatureArray]

a,-0.27724356949570034,-0.3096554106726788
b,-0.10619546908708237,0.07430127684522048
c,-0.07619665345406437,0.8474460146082116
d,0.19668718143436803,-0.8072994364484335

【问题讨论】:

    标签: python dataframe


    【解决方案1】:

    pandas.Dataframe.to_csv 方法有一个关键字参数 header=True 可以关闭以禁用标头。

    但是,它有时不起作用(根据经验)。 将它与index=False 结合使用应该可以解决您的问题。

    例如,这个 sn-p 应该可以解决您的问题:

    TempDay.to_csv('C:\MyFile.csv', index=False, header=False)
    

    这是一个完整的示例,展示了它如何禁用标题行:

    >>> import pandas as pd
    >>> import numpy as np
    >>> df = pd.DataFrame(np.random.randn(6,4))
    >>> df
              0         1         2         3 
    0  1.295908  1.127376 -0.211655  0.406262 
    1  0.152243  0.175974 -0.777358 -1.369432 
    2  1.727280 -0.556463 -0.220311  0.474878 
    3 -1.163965  1.131644 -1.084495  0.334077 
    4  0.769649  0.589308  0.900430 -1.378006 
    5 -2.663476  1.010663 -0.839597 -1.195599 
    
    >>> # just assigns sequential letters to the column
    >>> df[4] = [chr(i+ord('A')) for i in range(6)]
    >>> df
              0         1         2         3  4
    0  1.295908  1.127376 -0.211655  0.406262  A
    1  0.152243  0.175974 -0.777358 -1.369432  B
    2  1.727280 -0.556463 -0.220311  0.474878  C
    3 -1.163965  1.131644 -1.084495  0.334077  D
    4  0.769649  0.589308  0.900430 -1.378006  E
    5 -2.663476  1.010663 -0.839597 -1.195599  F
    
    >>> # here we reindex the headers and return a copy
    >>> # using this form of indexing just requires you to provide
    >>> # a list with all the columns you desire and in the order desired
    >>> df2 = df[[4, 1, 2, 3]]
    >>> df2
       4         1         2         3
    0  A  1.127376 -0.211655  0.406262
    1  B  0.175974 -0.777358 -1.369432
    2  C -0.556463 -0.220311  0.474878
    3  D  1.131644 -1.084495  0.334077
    4  E  0.589308  0.900430 -1.378006
    5  F  1.010663 -0.839597 -1.195599
    
    >>> df2.to_csv('a.txt', index=False, header=False)
    >>> with open('a.txt') as f:
    ...     print(f.read())
    ... 
    A,1.1273756275298716,-0.21165535441591588,0.4062624848191157
    B,0.17597366083826546,-0.7773584823122313,-1.3694320591723093
    C,-0.556463084618883,-0.22031139982996412,0.4748783498361957
    D,1.131643603259825,-1.084494967896866,0.334077296863368
    E,0.5893080536600523,0.9004299653290818,-1.3780062860066293
    F,1.0106633581546611,-0.839597332636998,-1.1955992812601897
    

    如果需要动态调整列,将最后一列移到第一列,可以如下操作:

    # this returns the columns as a list
    columns = df.columns.tolist()
    # removes the last column, the newest one you added
    tofirst_column = columns.pop(-1)
    # just move it to the start
    new_columns = [tofirst_column] + columns
    
    # then you can the rest
    df2 = df[new_columns]
    

    这仅允许您获取当前列列表,从当前列构造 Python 列表,并重新索引标题,而无需对标题有任何先验知识。

    【讨论】:

    • 感谢您的建议。更大的问题是它在 TemperatureArray 之后打印日期,但我需要在 TemperatureArray 之前的日期。
    • 你能提供一个小样本吗?我以为你主要是想关闭标题。除非您可以提供一个小的 Dataframe 作为示例,否则调试会有点困难。
    • 或者您只是在寻找按时间顺序排列的“之前”,即温度数组中日期之前发生的所有日期。
    • 我更新了上面的代码以显示我在寻找什么。谢谢
    • 已编辑,我正是这样做的:我在 Pandas 中使用列重新排序来返回数据帧的副本,然后我们将其转储到不带标题的文件中。
    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 2019-03-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    相关资源
    最近更新 更多