Reputation: 63
I have 3 lists id_list version_list and phases_list which i extracted using methods. My code is
a = pd.DataFrame(id_list, columns = ['ID'])
b = pd.DataFrame(version_list, columns = ['Version'])
c = pd.DataFrame(phases_list, columns = ['Required'])
x = pd.concat([a,b,c], axis=1, sort=False)
df2 = x.to_excel('df2.xlsx')
The output is
id version required
0 X Y Z 1 2 3 tur mir tur
But i want it like this
id version required
0 X 1 tur
1 Y 2 mir
2 Z 3 tur
How can I do this in Pandas?
Also one more issue is I extracted the data with Index Values so list is somewhat like 6 X 17 Y 20 Z also with version and required columns all have index values, how can i remove the index number from the list?
So now the
Upvotes: 0
Views: 101
Reputation: 28729
this is an alternative - i love @Yo_Chris's solution though :
m = zip(*[v.array[0].split() for k,v in df.items()])
pd.DataFrame(m).set_axis(df.columns,axis=1)
id version required
0 X 1 tur
1 Y 2 mir
2 Z 3 tur
Upvotes: 0
Reputation: 149175
What you want is probably the constructor using a dict:
df = pd.DataFrame({'ID': id_list, 'Version': version_list, 'Required': phases_list})
Upvotes: 0
Reputation: 14113
# stack your frame, split on your separator, reset_index and transpose
df.stack().str.split(' ', expand=True).reset_index(level=0, drop=True).T
id version required
0 X 1 tur
1 Y 2 mir
2 Z 3 tur
Upvotes: 1