Reputation: 672
I have csv dataset as :
5.1,3.5,1.4,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.3,3.3,6.0,2.5,Iris-virginica
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
5.1,2.5,3.0,1.1,Iris-versicolor
5.7,2.8,4.1,1.3,Iris-versicolor
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3.0,5.9,2.1,Iris-virginica
6.3,2.9,5.6,1.8,Iris-virginica
I want to have it as:
Iris-setosa,Iris-versicolor,Iris-virginica
5.1,3.5,1.4,
7.0,3.2,4.7,
6.3,3.3,6.0,
4.9,3.0,1.4,
4.7,3.2,1.3
I searched and there are lot of answers like using pd.pivot but failed to have desired output. When I tried giving some index A to pivot, it thorw 'index has duplicate entries'. If answer contains .pivot function, then can you please explain me what actually index, column and values are.
Upvotes: 1
Views: 66
Reputation: 862661
I believe you need filter columns by positions - first 3 by DataFrame.iloc
and if necessary set columns names by list
:
df = df.iloc[:, :3]
df.columns = ['Iris-setosa','Iris-versicolor','Iris-virginica']
print (df)
Iris-setosa Iris-versicolor Iris-virginica
0 5.1 3.5 1.4
1 7.0 3.2 4.7
2 6.3 3.3 6.0
3 4.9 3.0 1.4
4 4.7 3.2 1.3
5 4.4 2.9 1.4
6 5.1 2.5 3.0
7 5.7 2.8 4.1
8 5.8 2.7 5.1
9 7.1 3.0 5.9
10 6.3 2.9 5.6
Upvotes: 2