【问题标题】:Is there a way to combine multiple columns in Pandas that can join all data from certain columns into one column (12 columns to 1)有没有办法在 Pandas 中组合多个列,可以将某些列中的所有数据合并为一列(12 列到 1)
【发布时间】:2020-10-05 02:49:54
【问题描述】:

我希望这个问题很清楚,但我正在调查 Zillow 房屋销售数据,并且无法将某些年份的所有月份合并到新声明的“年份”变量中。这实际上应该将所有具有 Jan-YY、Feb-YY、Mar-YY...等的数据存储到 YY。

我尝试过 Pandas 内置的函数,例如 Stack()Pivot(),但这些似乎都不起作用。

如果没有可行的方法来做到这一点,我有什么替代方案?提前致谢!

示例: 取 Column1 = '1/31/1996' 和 Column2 = '2/28/1996'...等。和 Column12 = '12/31/1996' 并组合成一个新的 名为 Y1996 的列。这将比每月细分更容易分析。

我的代码:

  import pandas as pd
  import numpy as np
  import statsmodels.api as sm
  import matplotlib.pyplot as plt 
  %matplotlib inline

  zil = pd.read_csv('zillow.csv')
  df_zil = pd.DataFrame(df_zil)
  df_zil.head(4)

  #My attempt at merging into one
  y1996 = (df_zil['1/31/1996'] + df_zil['3/31/1996'] + df_zil['4/30/1996'] + df_zil['5/31/1996'] + 
  df_zil['6/30/1996'] + df_zil['7/31/1996'] + df_zil['8/31/1996'] + df_zil['9/30/1996'] + 
  df_zil['10/31/1996'] + df_zil['11/30/1996'] + df_zil['12/31/1996'])

Screen shot of how data is formatted via excel -- Starts on Column I

参考 Zillow 数据:https://www.zillow.com/research/data/

【问题讨论】:

    标签: python pandas csv zillow


    【解决方案1】:

    我认为您需要与枢轴相反的东西 - melt。您的数据采用“宽”格式,如果将数据转换为“高”格式,则更容易进行此摘要。获得 tall 格式的数据后,您可以使用 groupby 来汇总同一年内的值。

    我下载了 House Inventory and Sales 数据集,并编写了一个小程序来总结同一年的所有值。

    代码:

    import pandas as pd
    
    df = pd.read_csv("Metro_invt_fs_uc_sfrcondo_smoothed_month.csv")
    # Take all of the columns after the index and convert them into additional rows
    df = df.melt(id_vars=["RegionID", "SizeRank", "RegionName", "RegionType", "StateName"], var_name="Date")
    # Drop date, but keep year
    df["Year"] = pd.to_datetime(df["Date"]).dt.year
    df = df.drop("Date", axis="columns")
    # Aggregate each year
    df = df.groupby(["RegionID", "SizeRank", "RegionName", "RegionType", "StateName", "Year"], as_index=False).sum()
    print(df)
    

    输出:

         RegionID  SizeRank                         RegionName RegionType StateName  Year    value
    0      394304        74                          Akron, OH        Msa        OH  2017   3576.0
    1      394304        74                          Akron, OH        Msa        OH  2018  42625.0
    2      394304        74                          Akron, OH        Msa        OH  2019  39078.0
    3      394304        74                          Akron, OH        Msa        OH  2020  21532.0
    4      394308        60                         Albany, NY        Msa        NY  2017   2969.0
    ..        ...       ...                                ...        ...       ...   ...      ...
    475    753906        75  North Port-Sarasota-Bradenton, FL        Msa        FL  2020  73953.0
    476    753924        54                 Urban Honolulu, HI        Msa        HI  2017   3735.0
    477    753924        54                 Urban Honolulu, HI        Msa        HI  2018  50079.0
    478    753924        54                 Urban Honolulu, HI        Msa        HI  2019  57413.0
    479    753924        54                 Urban Honolulu, HI        Msa        HI  2020  35522.0
    
    [480 rows x 7 columns]
    

    【讨论】:

      猜你喜欢
      • 2013-12-14
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2018-11-10
      • 2016-09-22
      • 2017-03-25
      相关资源
      最近更新 更多