Ad

How To Read One Column Data As One By One Row In Csv File Using Python

- 1 answer

Here I have a dataset with three inputs. Three inputs x1,x2,x3. Here I want to read just x2 column and in that column data stepwise row by row.

Here I wrote a code. But it is just showing only letters.

Here is my code

data = pd.read_csv('data6.csv')
row_num =0
x=[]
for col in data:
if (row_num==1):
    x.append(col[0])
row_num =+ 1
print(x)

result : x1,x2,x3

What I expected output is:

expected output x2 (read one by one row)
65
32
14
25
85
47
63
21
98
65
21
47
48
49
46
43
48
25
28
29
37

Subset of my csv file :

x1  x2  x3
6   65  78
5   32  59
5   14  547
6   25  69
7   85  57
8   47  51
9   63  26
3   21  38
2   98  24
7   65  96
1   21  85
5   47  94
9   48  15
4   49  27
3   46  96
6   43  32
5   48  10
8   25  75
5   28  20
2   29  30
7   37  96

Can anyone help me to solve this error?

Ad

Answer

If you want list from x2 use:

x = data['x2'].tolist()
Ad
source: stackoverflow.com
Ad