【问题标题】:Pandas DataFrame combine multi row spanning columnPandas DataFrame 组合多行跨列
【发布时间】:2020-09-01 15:24:57
【问题描述】:

我有一个复杂的抓取数据框,如下所示:

就上下文而言,PDF 中的原始数据如下所示:

数据帧信息:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 26 entries, 0 to 25
Data columns (total 5 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   0       26 non-null     object
 1   1       26 non-null     object
 2   2       26 non-null     object
 3   3       26 non-null     object
 4   4       26 non-null     object
dtypes: object(5)
memory usage: 1.1+ KB

我希望数据如下图所示。在1 (Transaction Type) 列中带有SP 的每一行都应将0 (Asset) 列连接在一起,直到下一个SP 出现在1 列中:

我该怎么做?

【问题讨论】:

    标签: python pandas dataframe web-scraping pdf-scraping


    【解决方案1】:

    下面的代码为您提供每一列 1,其中行不是 None(意味着它有一个 S 或 P)您的 q 的其他部分我无法理解。

    import pandas as pd
    df = pd.read_csv('filename.csv')
    dfs = df['0'][(df['1']=="S")]
    dff = df['0'][(df['1']=="P")]
    print("Sdays", " ".join(dfs))
    print("Fdays", " ".join(dff))
    
    I think now its ok.
    

    【讨论】:

    • 我添加了图片来澄清。
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2012-06-13
    • 2018-02-20
    • 2018-04-08
    • 2015-01-24
    • 2016-11-22
    • 1970-01-01
    相关资源
    最近更新 更多