Reputation: 113
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.
Upvotes: 2
Views: 1292
Reputation: 323326
This is more like a hidden pivot
problem
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
Upvotes: 2
Reputation: 294488
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
Upvotes: 4
Reputation: 3301
use the fillna method to fill missing values with your choosing.
df['Condition'] = df['Condition'].fillna('')
Upvotes: 1