python - Pandas DataFrame - Adding rows to df based on data in df -
apologies not specified title. i've been, unsuccesfully far, trying come way add new 'rows' pandas dataframe based on contents of of columns. hope make clear example. data mock-up data suffices in painting bigger picture.
so, lets car dealer has, among others, following 7 customers. in dataframe can see customer-id, gender (because why not), , country live in. in addition, can see whether they've bought of 4 car brands (and type of car) or not (na) (all values in dataframe strings btw). example, customer 4 female russia, , has bought porsche 911 dealer.
cust-id sex country audi ferrari porsche jaguar 0 cu1 f fr r8 ff na na 1 cu2 m na na na xf 2 cu3 m uk rs7 na na na 3 cu4 f ru na na 911 na 4 cu5 m na na 918 ford 5 cu6 f s6 na na f-type 6 cu7 m uk a8 na macans xe
what i'd able create new rows cases customer has bought more 1 car, each row specifying 1 car, , other car brand columns saying 'na' in specific row. above example result in following dataframe.
cust-id sex country audi ferrari porsche jaguar 0 cu1 f fr r8 na na na 1 cu1 f fr na ff na na 2 cu2 m na na na xf 3 cu3 m uk rs7 na na na 4 cu4 f ru na na 911 na 5 cu5 m na na 918 na 6 cu5 m na na na ford 7 cu6 f s6 na na f-type 8 cu7 m uk a8 na na na 9 cu7 m uk na na macans na 10 cu7 m uk na na na xe
this means original row 3 cars specified lead 3 new rows each specifying 1 of cars (with original row gone). cust-id, sex, , country values not change. first time using website ask question myself formatting not bad. appreciate help/guidance. python pandas dataframe
the way approach following:
iterate on every
car
column , keep records have non-null valuesdf_dict = {} car in ['audi', 'ferrari', 'porsche' ,'jaguar']: non_nulls = df[ df.apply(lambda x: not pd.isnull(x[car] ), axis=1)] df_dict[car] = non_nulls[[cust-id,sex,country, car]]
concatenate dataframes
pd.concat
, create nulls in right placesfinal_df = pd.concat( df_dict.values() )
something along lines should work. did not test code though, use own judgement!
Comments
Post a Comment