【问题标题】:How to split data that is separated by headers into different chunks?如何将由标头分隔的数据拆分为不同的块?
【发布时间】:2019-07-28 07:09:05
【问题描述】:

我有一组数据,每行由一个标题分割。我想用 pandas 读取这个文件,并在一个新的标题之后将它拆分为多个数组。标题之间的行数不一定相等,因此理想情况下,它会检测标题,跳过它并开始一个新的数据帧,或者将该行的索引写入列表或其他东西,以便我可以将其切片之后。

我尝试过这样的事情:

pd.read_csv('test.csv', skiprows=list_of_skipped_rows)

但这很不方便,因为我必须创建 list_of_skipped_rows 手动。

数据看起来像这样:

spin 1: WF(Fe3_3d-2) -> WF(Fe3_3d-2) at relative 
T=      0.00000      0.00000      0.00000  hop=            -0.458880333440493 +i*              0.000000000000000
T=      0.00000      7.27841      0.00000  hop=            -0.035603658911014 +i*             -0.000000000000000
T=     14.55682      7.27841      0.00000  hop=             0.002829331916122 +i*              0.000000000000000
spin 1: WF(Fe3_3d-2) -> WF(Fe3_3d-1) at relative 
T=      0.00000      7.27841      0.00000  hop=            -0.032224030850531 +i*             -0.000000000000001
T=      0.00000     -7.27841      0.00000  hop=             0.032224030850531 +i*              0.000000000000001
T=     -7.27841      0.00000      0.00000  hop=            -0.042422160597321 +i*              0.000000000000001
T=      7.27841      0.00000      0.00000  hop=             0.042422160597321 +i*             -0.000000000000001
T=     -7.27841     -7.27841      0.00000  hop=            -0.038244803420008 +i*              0.000000000000001
T=     -7.27841      7.27841      0.00000  hop=             0.001458007120899 +i*              0.000000000000000
T=      7.27841      7.27841      0.00000  hop=             0.038244803420008 +i*             -0.000000000000001
spin 1: WF(Fe3_3d-2) -> WF(Fe3_3d+0) at relative 
T=      0.00000      0.00000      0.00000  hop=             0.241909440386978 +i*             -0.000000000000001
T=      0.00000      7.27841      0.00000  hop=            -0.032644583985555 +i*             -0.000000000000001
T=      0.00000     -7.27841      0.00000  hop=            -0.032644583985555 +i*             -0.000000000000001
T=     -7.27841      0.00000      0.00000  hop=             0.055254423473069 +i*             -0.000000000000001
T=      7.27841      0.00000      0.00000  hop=             0.055254423473069 +i*             -0.000000000000001
T=     -7.27841     -7.27841      0.00000  hop=            -0.028268235984415 +i*             -0.000000000000001
T=     14.55682      7.27841      0.00000  hop=             0.005804248461754 +i*             -0.000000000000001
spin 1: WF(Fe3_3d-2) -> WF(Fe3_3d+1) at relative 
T=      0.00000      7.27841      0.00000  hop=            -0.032224030850531 +i*             -0.000000000000001
T=      0.00000     -7.27841      0.00000  hop=             0.032224030850531 +i*              0.000000000000001
T=     -7.27841      0.00000      0.00000  hop=             0.042422160597321 +i*             -0.000000000000001
.....................

【问题讨论】:

  • 你可以用普通的Python函数读取文件,识别和统计标题行。然后用它来引导csv 格式化读取。 pd.read_csv 有很多参数,但它们无法解释所有变化。

标签: python-3.x pandas csv numpy


【解决方案1】:

对我来说,这听起来有点难以使用pd.read_csv 提供的开箱即用功能。我可能会这样做的方法是逐行手动读取文件,并使用正则表达式来检测我何时点击标题行,因为它们似乎都以特定方式格式化。在点击标题行后,我将创建一个新的 DataFrame 并逐行添加。这是一种可能性:

list_of_dataframes = []
current_df = pd.DataFrame() # you may want to pass in column names here
with open("data.csv") as file:
    for line in file:
        if isheader(line): # define some function that checks if it's a header. If so, skip line; create new dataframe.
            list_of_dataframes.append(current_df)
            current_df = pd.DataFrame()
            continue
        tokens = line.split(",") # or whatever delimiter you're using
        current_df = current_df.append(pd.Series(tokens, index=current_df.columns), ignore_index=True)
list_of_dataframes.append(current_df)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多