python - Repeating items in a data frame using pandas -


i have following dataframe:

id  z2  z3  z4   1   2     fine  2   7   b   3   9   c   delay 4   30  d   cold 

i going generate data frame repeating each item in row twice except items in column z4 (that should not repeated). how can using python , pandas.

the output should this:

id  z1  z3  z4 1   2     fine  1   2      1   2      2   7   b   2   7   b    2   7   b    3   9   c   delay 3   9   c    3   9   c    4   30  d   cold 4   30  d    4   30  d    

another way use indexing: notice df.iloc[[0, 1, 2, 3]*2, :3] give 2 copies of first 3 columns.

this can appended original df. remove na. sort on index values , reset index (dropping old index). of can chained:

df.append(df.iloc[[0, 1, 2, 3]*2, :3]).fillna('').sort_index().reset_index(drop=true) 

which produces:

    id  z2 z3     z4 0    1   2    fine 1    1   2         2    1   2         3    2   7  b   4    2   7  b        5    2   7  b        6    3   9  c  delay 7    3   9  c        8    3   9  c        9    4  30  d   cold 10   4  30  d        11   4  30  d 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -