当值的长度与索引的长度不匹配时,用 Nan 填充

Fill with Nan when Length of values does not match length of index

我想要一个看起来像这样的数据框。

 a         b
cars      New
bikes     nan
trains    nan

假设如下...

list(oldDF["Transportation"].unique())=["cars", "bikes", "trains"]
list(oldDF["Condition"].unique())=["New"]

我的代码目前看起来像这样:

newList=["Transportation", "Condition"]
newDF=pf.DataFrame(columns=newList)

for i in newList:
    newDF[i]= list(oldDF[i].unique())

我希望能够打印上面的数据框并用 nan 填充缺失值,而不是出现值错误。

使用 fillna 方法根据您的选择填充缺失值。

df['Condition'] = df['Condition'].fillna('')

这更像是一个隐藏的 pivot 问题

oldDF.melt().drop_duplicates().\
      assign(index=lambda x : x.groupby('variable').cumcount()).\
            pivot('index','variable','value')
Out[62]: 
variable Condition Transportation
index                            
0              New           cars
1              NaN          bikes
2              NaN         trains

from_dictorient='index'

pd.DataFrame.from_dict({n: c.unique() for n, c in oldDF.iteritems()}, orient='index').T

  Transportation Condition
0           cars       New
1          bikes      None
2         trains      None

zip_longest

from itertools import zip_longest

pd.DataFrame([*zip_longest(*map(pd.unique, map(oldDF.get, oldDF)))], columns=[*oldDF])

  Transportation Condition
0           cars       New
1          bikes      None
2         trains      None