python - Merge multiple rows in Pandas -
i have dataset 5 rows wish merge 1 can use them unique column identifiers. example
name unique no. summary nominal voltage nominal voltage upstream upstream nan nan class upstream downstream constraint oppurtunity (non unique) nan nan nan nan physical nan
i columns named
name (non unique) unique no. summary class nominal voltage upstream nominal voltage downstream upstream constraint phsyical upstream oppurtunity
so rows (there 5) merged (while ignoring nans) use unique column names.
thanks in advance.
as far can understand, groupby requires common between things being grouped, can't used here? whole database of string type because thought make easier join them, couldn't figure out way.
i think need apply
dropna
:
df.columns = df.apply(lambda x: ' '.join([x.name] + x.dropna().tolist())) print (df.columns.tolist()) ['name (non unique)', 'unique no.', 'summary class', 'nominal voltage upstream', 'nominal voltage downstream', 'upstream constraint physical', 'upstream oppurtunity nan']
if there string
nan
- replace
first:
df.columns = df.replace('nan',np.nan) .apply(lambda x: ' '.join([x.name] + x.dropna().tolist())) print (df.columns.tolist()) ['name (non unique)', 'unique no.', 'summary class', 'nominal voltage upstream', 'nominal voltage downstream', 'upstream constraint physical', 'upstream oppurtunity']
but if need unique column names, simpliest is:
df.columns = range(len(df.columns)) print (df.columns.tolist()) [0, 1, 2, 3, 4, 5, 6]
or assign new unique values of columns:
df.columns = list('abcdefg') print (df.columns.tolist()) ['a', 'b', 'c', 'd', 'e', 'f', 'g']
Comments
Post a Comment