Search code examples
pythonpandasvalueerror

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


I want a dataframe that looks like this.

 a         b
cars      New
bikes     nan
trains    nan

Assume the following...

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

My Code Looks like this Currently:

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

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

I want to be able to print the dataframe above and fill missing values with nan rather than getting a value error.


Solution

  • from_dict and orient='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