Reputation: 101
I am totally new to using python I am using a data frame in which one row needs to be split into two rows. Both the rows will have same data except for one column
Upvotes: 1
Views: 110
Reputation: 30920
Another approach:
new_df = (df.reindex(df.index.repeat(len(df.columns)-1))
.assign(Column4 = df[df.columns.difference(['Column1'])].stack().values))
print(new_df)
Column1 Column2 Column3 Column4
0 1 A1 A2 A1
0 1 A1 A2 A2
1 2 B1 B2 B1
1 2 B1 B2 B2
Upvotes: 1
Reputation: 75080
here is another way without apply
using df.stack
, series.droplevel
followed by df.join
:
m = df.set_index('Column1')
m = m.join(m.stack().droplevel(1).to_frame('Column4')).reset_index()
print(m)
Column1 Column2 Column3 Column4
0 1 A1 A2 A1
1 1 A1 A2 A2
2 2 B1 B2 B1
3 2 B1 B2 B2
Upvotes: 2
Reputation: 25189
Suppose we have a df:
df
Column1 Column2 Column3
0 1 A1 A2
1 2 B1 B2
Then we can do:
df["Column4"] = df.apply(lambda x: [x[1],x[2]], axis=1)
df = df.explode("Column4")
print(df)
Column1 Column2 Column3 Column4
0 1 A1 A2 A1
0 1 A1 A2 A2
1 2 B1 B2 B1
1 2 B1 B2 B2
If contents of Column1
is an issue we can go one step further:
df["Column1"] = range(1, df.shape[0]+1)
print(df)
Column1 Column2 Column3 Column4
0 1 A1 A2 A1
0 2 A1 A2 A2
1 3 B1 B2 B1
1 4 B1 B2 B2
Upvotes: 3