1

I use python, matplotlib and pandas to filter my log file.

My log file contains two columns like this:

column_name = ["TIME", "IDPKT"]

The second column "IDPKT" contains id of packets in my simulation which have the following form: 1.1 ; 1.2 ; 1.10 ; etc. So, ths Ids 1.1 and 1.10 are two different packets (1.1 is the first packet generated by the node 1 and 1.10 is the tenth packet generated also by the node 1).

So, python reads automatically this column as float numbers. The problem that it considers the different ids 1.10 ; 1.100 ; 1.1000 as 1.1.

This issue causes a big problem in my work since they are different packets generated and it calculates only 1.1.

Can someone help me to solve this problem please?

6
  • Someone may be able to help you if you show how you load the data. See minimal, complete, and verifyable example. Commented Mar 23, 2017 at 14:01
  • I think it's better to use strings rather than floats if you want to keep all their decimal places Commented Mar 23, 2017 at 14:02
  • Thank you for your response, @kazemakase I load my data like this: df = pd.read_csv(filename, sep=";", header=None, names=column_name) Commented Mar 23, 2017 at 14:03
  • @Mr.Xcoder Thank you for your answer, in my file there are defined like this: 12.25 ; 1.1 (12.25 is the time and 1.1 is the id packet), how can I force python to read 1.1 as string? Commented Mar 23, 2017 at 14:05
  • @Myriam Great! In that case you can find a solution in the q/a linked above (first comment) - have a look at the dtype argument. Commented Mar 23, 2017 at 14:06

1 Answer 1

1

The problem is that you are reading the data in as a float (pandas does this automatically because it LOOKS like a float), but you want to treat it like a string. You can specify the data type when you read it in, e.g.:

df = pd.read_csv(filename, sep=";", header=None, names=column_name, dtype={'IDPKT':'str'})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your reply, it works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.