大佬教程收集整理的这篇文章主要介绍了无法将列表转换为数据框。不断收到错误“ValueError: Must pass 2-d input. shape=(1, 4, 5)”,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须有 2 个 dfs:
@H_262_5@dfMiss:
和
@H_262_5@dfSuper:
我需要创建一个最终输出,总结我能够在下面的代码中显示的 2 个表中的数据:
dfcity = dfSuper \
.groupby(by='City').count() \
.drop(columns='Superhero ID') \
.rename(columns={'Superhero': '@R_974_10586@l count'})
print("This is the df city : ")
print(dfcity)
## Convert column MissionEndDate to datetiR_291_11845@e format
for df in dfMiss:
# Dates arE interpreted as MM/dd/yyyy by default,dayfirst=false
df['Mission End date'] = pd.to_datetiR_291_11845@e(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 @R_974_10586@l no. superheros per city
for col in df.columns[1:]:
df[col] = df['@R_974_10586@l count'] - df[col]
dfFree += [df.astype(int)]
print(dfFreE)
dfResult = pd.DataFrame(dfFreE)
问题是当我尝试将 DfFree 转换为数据帧时出现错误:
@H_262_5@"ValueError: 必须通过二维输入。shape=(1,4,5) "
引发错误的行是
dfResult = pd.DataFrame(dfFreE)
有人知道这意味着什么以及我如何将列表转换为 df 吗?
谢谢:)
使用 SOLID 分隔您的代码。关注点分离。读起来不容易
sid=[665544,665544,2121,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,555555,666666,432432]
hero=["SpideRMAN","Ironman","Batman","Dr. Strange","Thor","SupeRMAN","Nightwing","Loki","Wolverine"]
city=["New York","New York","Gotham","Asgard","Metropolis","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_datetiR_291_11845@e(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())
以上是大佬教程为你收集整理的无法将列表转换为数据框。不断收到错误“ValueError: Must pass 2-d input. shape=(1, 4, 5)”全部内容,希望文章能够帮你解决无法将列表转换为数据框。不断收到错误“ValueError: Must pass 2-d input. shape=(1, 4, 5)”所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。