Ask Your Question
0

import csv file: integers appearing as floating points

asked 11 years ago

GingerAndy gravatar image

updated 11 years ago

vdelecroix gravatar image

Hi,

I am trying to import a csv file in order to create an adjacency matrix and then plot the associated directed graph. I upload the csv file and use its content to generate a matrix as follows

sage: import csv                           
sage: data = list(csv.reader(file(DATA+'matrix23.csv')))
sage: m = matrix([[ float(_) for _ in line] for line in data])
sage: m

Whereas my csv file only contains integers 1's and 0's, the generated matrix is floating point 1.0's and 0.0's!

Note: the CSV file was created using MS Excel with cells formated to zero decimal places. I'm new to both python and sage and can't find anything on the forums to help

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
1

answered 11 years ago

tmonteil gravatar image

updated 11 years ago

If your matrix m only contains entries like 0.O and 1.0, you can try to transform it back to an integer matrix:

sage: n = m.change_ring(ZZ)

You can also transform your entries into integers while creating your matrix:

sage: m = matrix([[ ZZ(_) for _ in line] for line in data])
Preview: (hide)
link
0

answered 11 years ago

GingerAndy gravatar image

Thanks that works a treat.

Preview: (hide)
link

Comments

Welcome to Sage! If you are pleased with Thierry answer then you should add it as a selected answer. Moreover this message to Thierry answer might be a comment within its answer (answer are somewhat reserved to *answer* the question and comments to *comment* about the questions or the answers). The last but not the least: you may select Thierry's answer as your selected answer in order to close this post!

vdelecroix gravatar imagevdelecroix ( 11 years ago )
0

answered 11 years ago

vdelecroix gravatar image

Hi,

Note that in your code you use "float" to convert a string to a number. But by its very definition this function builds a floating point.

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 11 years ago

Seen: 1,484 times

Last updated: May 20 '13