0

I have some data in Microsoft excel that I save them as CSV file for ease of use. the data Structure is like this:

MS Excel format:

L1                      
0   1   0   0   0   1   1
0   0   1   0   0   1   0
0   0   0   1   0   0   1
0   0   0   0   1   0   0
1   1   1   1   1   1   1
1   1   1   1   1   1   1
1   1   1   1   1   1   1
1   1   1   1   1   1   1

CSV format

L1,,,,,,,,,,,,,,
0,1,0,0,0,1,1,
0,0,1,0,0,1,0,
0,0,0,1,0,0,1,
0,0,0,0,1,0,0,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,

As you see only the first column has label now I want to read the CSV file (or it's easier the excel file) to get each column and do some bit manipulation operation on them. How can I acheive this? I have read something about pandas But I can't find anything useful in order to fetch each coloumn

4

4 回答 4

2

给定 .csv 文件 temp.csv

L1x,,,,,,,
0,1,0,0,0,1,1,
0,0,1,0,0,1,0,
0,0,0,1,0,0,1,
0,0,0,0,1,0,0,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,

阅读如下:

import pandas
a = pandas.read_csv('temp.csv', names = ["c%d" % i for i in range(8)], skiprows = 1)
a

输出:

   c0  c1  c2  c3  c4  c5  c6  c7
0   0   1   0   0   0   1   1 NaN
1   0   0   1   0   0   1   0 NaN
2   0   0   0   1   0   0   1 NaN
3   0   0   0   0   1   0   0 NaN
4   1   1   1   1   1   1   1 NaN
5   1   1   1   1   1   1   1 NaN
6   1   1   1   1   1   1   1 NaN
7   1   1   1   1   1   1   1 NaN

最后一列中的“NaN”来自讨厌的尾随逗号。范围内的 8 需要与列数匹配。a要访问正在使用的列

a.c3

或者

a[c3]

两者都导致

0    0
1    0
2    1
3    0
4    1
5    1
6    1
7    1
Name: c3

pandas 很酷的一点是,如果你想对两列进行异或运算,你可以非常简单。

a.c0^a.c2

输出

0    0
1    1
2    0
3    0
4    0
5    0
6    0
7    0
Name: c0
于 2013-11-06T17:16:53.783 回答
1

假设我有:

在此处输入图像描述

您可以将其保存到如下所示的 CSV 文件中:

L1,,,
L2,0,10,20
L3,1,11,21
L4,2,12,22
L5,3,13,23
L6,4,14,24
L7,5,15,25
L8,6,16,26
L9,7,17,27
L10,8,18,28

要获取任何 col,请使用 CSV 阅读器并使用 zip 转置:

import csv

with open('test.csv', 'rU') as fin:
    reader=csv.reader(fin)
    data=list(reader)

print 'data:', data
# data: [['L1', '', '', ''], ['L2', '0', '10', '20'], ['L3', '1', '11', '21'], ['L4', '2', '12', '22'], ['L5', '3', '13', '23'], ['L6', '4', '14', '24'], ['L7', '5', '15', '25'], ['L8', '6', '16', '26'], ['L9', '7', '17', '27'], ['L10', '8', '18', '28']]

请注意,数据是行列表。您可以使用 zip 转置该列表列表以获取列列表:

trans=zip(*data)
print 'trans:',trans
# trans: [('L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8', 'L9', 'L10'), ('', '0', '1', '2', '3', '4', '5', '6', '7', '8'), ('', '10', '11', '12', '13', '14', '15', '16', '17', '18'), ('', '20', '21', '22', '23', '24', '25', '26', '27', '28')]

然后只需索引以获取特定列:

print trans[0]
# ('L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8', 'L9', 'L10')

当然,如果您想对单元格进行算术运算,则需要根据需要将字符串转换为整数或浮点数。

于 2013-11-06T16:29:12.713 回答
0

Sample Code returns column as array.:

input = """L1,,,,,,,,,,,,,,
0,1,0,0,0,1,1,
0,0,1,0,0,1,0,
0,0,0,1,0,0,1,
0,0,0,0,1,0,0,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1,
"""

def getColumn(data,column_number):

    dump_array=[]
    lines=data.split("\n")
    for line in lines:
        tmp_cell = line.split(",")
        dump_array.append(tmp_cell[3])
    return dump_array

#for ex. get column 3
getColumn(3,input)

This may give an idea to manuplate your grid...

Note: I dont have an interpreter for testing code now, so sorry if there is typo...

于 2013-11-06T16:01:56.553 回答
0
import pandas as pd
pd.read_excel("foo.xls", "Sheet 1",
              names=["c%d" % i for i in range(7)])

输出:

   c0  c1  c2  c3  c4  c5  c6
0   0   1   0   0   0   1   1
1   0   0   1   0   0   1   0
2   0   0   0   1   0   0   1
3   0   0   0   0   1   0   0
4   1   1   1   1   1   1   1
5   1   1   1   1   1   1   1
6   1   1   1   1   1   1   1
7   1   1   1   1   1   1   1
于 2013-11-06T16:00:06.110 回答