无法将列表转换为数据帧。继续获取错误“ValueError:必须传递2d输入。形状=(1,4,5)”

2024-04-18 22:14:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须做两件事:

dfMiss:

enter image description here

dfSuper:

enter image description here

我需要创建一个最终输出,总结两个表中的数据,如下代码所示:

dfCity = dfSuper \
    .groupby(by='City').count() \
    .drop(columns='Superhero ID') \
    .rename(columns={'Superhero': 'Total count'})

print("This is the df city : ")
print(dfCity)



## Convert column MissionEndDate to DateTime format
for df in dfMiss:
    # Dates are interpreted as MM/dd/yyyy by default, dayfirst=False
    df['Mission End date'] = pd.to_datetime(df['Mission End date'], dayfirst=True)

    # Get Year and Quarter, given Q1 2020 starts in April
    date = df['Mission End date'] - pd.DateOffset(months=3)
    df['Mission End quarter'] = date.dt.year.astype(str) + ' Q' + date.dt.quarter.astype(str)

## Get no. Superheros working per City per Quarter
dfCount = []

for dfM in dfMiss:
    # Merge DataFrames
    df = dfSuper.merge(dfM, left_on='Superhero ID', right_on='SID')

 
    df = df.pivot_table(index=['City', 'Superhero'], columns='Mission End quarter', aggfunc='nunique')

    # Get the first group (all the groups have the same values)
    df = df[df.columns[0][0]]

    # Group the values by City (effectively "collapsing" the 'Superhero' column)
    df = df.groupby(by=['City']).count()

    dfCount += [df]

## Get no. Superheros available per City per Quarter
dfFree = []

for dfC in dfCount:

    # Merge DataFrames
    df = dfCity.merge(right=dfC, on='City', how='outer').fillna(0)  # convert NaN values to 0

    # Subtract no. working superheros from total no. superheros per city
    for col in df.columns[1:]:
        df[col] = df['Total count'] - df[col]
    dfFree += [df.astype(int)]

    print(dfFree)
dfResult = pd.DataFrame(dfFree)

问题是,当我尝试将DfFree转换为数据帧时,会出现以下错误:

“ValueError:必须通过二维输入。形状=(1,4,5)”

引发错误的行是

dfResult = pd.DataFrame(dfFree)

有人知道这意味着什么,以及我如何将列表转换为df吗

谢谢:)


Tags: columnstheincitydffordateby
1条回答
网友
1楼 · 发布于 2024-04-18 22:14:26

使用SOLID分隔代码。关注点分离。这不容易读

 sid=[665544,665544,2121,665544,212121,123456,666666]
 mission_end_date=["10/10/2020", "03/03/2021", "02/02/2021", "05/12/2020", "15/07/2021", "03/06/2021", "12/10/2020"]

 superherod_sid=[212121,364331,678523,432432,665544,123456,555555,666666,432432]
 hero=["Spiderman","Ironman","Batman","Dr. Strange","Thor","Superman","Nightwing","Loki","Wolverine"]
 city=["New York","New York","Gotham","New York","Asgard","Metropolis","Gotham","Asgard","New York"]

 df_mission=pd.DataFrame({'sid':sid,'mission_end_date':mission_end_date})
 df_super=pd.DataFrame({'sid':superherod_sid,'hero':hero, 'city':city})

 df=df_super.merge(df_mission,on="sid", how="left")
 df['mission_end_date']=pd.to_datetime(df['mission_end_date'])
 df['mission_end_date_quarter']=df['mission_end_date'].dt.quarter
 df['mission_end_date_year']=df['mission_end_date'].dt.year
 print(df.head(20))

 pivot = df.pivot_table(index=['city', 'hero'], columns='mission_end_date_quarter',  aggfunc='nunique').fillna(0)
 print(pivot.head())

相关问题 更多 >