4

I am a python user in the very early stage.

I have two data set of temperature over a specific location from the year 1850 to 2010 with one value of temperature for each month for this entire period. I am trying to create a table with these values in the below given format. T is my data.

year data JAn FEB MAR APR MAY JUN JUL AUG SEP .......DEC.
1850 data1 t   t   t   t   t   t   t   t   t          t.
     data2 t   t   t   t   t   t   t   t   t          t.
'.
'.
'.
2010 data1 t   t   t   t   t   t   t   t  t           t.

I am sorry i cannnot post a picture of the table how i need. I am not allowed to post a picture. and i am not able to specify the shape of my table i need. so am posting a link of another sample table. its another data set. but i need to have two rows against the year. one for my data1 and one for my data 2.

Now what i have is the complete one series of data ranging from 1850 to 2010. I want to rewrite the two data sets in the above given format as a table. from the data i have sliced data1 and data 2 for each year. I know it is an easily accomplish able job through an office package, but i know that is not the way of programing. please some one help me in doing it.

Here is what i have now.

data1 = [t, t, t, t, t, t, t, t, t,..............................t]
data2 = [t, t, t, t, t, t, t, t, t,..............................t]

#This data1 and data2 is the list of data for the entire period from 1850-2010
#I sliced this data as
n = len(data1)
data1_yearly = [data1[i:i+12] for i in xrange(0,n,12)]
data2_yearly = [data2[i:i+12] for i in xrange(0,n,12)]

Now I have the values for both data1 and data2 sliced for each year. data1_yearly[0] gives me the value of the data for the year 1850 and further indexing will give me the data for all the period.

So from here starts my problem. How can I write this data as a table in the format I specified above. I am purely new to this language and so please don't consider this request a foolish one and kindly hlep me.

6
  • 1
    You need to be more specific about what you mean by "a table." Are you talking about a text file that looks like your first snippet, or do you mean entries in a relational database, or what? Commented May 25, 2011 at 19:28
  • I'm not sure how helpful it is, but you should have a look at numpy. Commented May 25, 2011 at 19:28
  • I am sorry if am not giving the description correctly. Printing a text file or just giving a list in that shape is ok for me. I am beginer to numpy and python, and i tried lot of wrong things to add the data in the desired rows and columns. Commented May 25, 2011 at 19:29
  • Please be more precise: is the problem that you want to parse data in a given input format into Python lists, that you want to render Python lists into an output format, or both? Also, if you are trying to convert a particular file, you'll get better answers if you provide a link to your source for the file. Commented May 25, 2011 at 19:29
  • Saha now i have on single series of data set ranging from 1850 january to 2010 december. I want to rewrite that data, as rows and columns and in rows i should have the data for an year and in columns i shoul have the month. I will edit my question and will add the data file i have now. Commented May 25, 2011 at 19:36

2 Answers 2

2

I would recommend that you have a look at string templates

Example:

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
[...]
ValueError: Invalid placeholder in string: line 1, col 10
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
[...]
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'

If you create a string template of the target format and then place the data in a dictionary as described above, the conversion should be easy.

That is, if I interpreted your question correctly, i.e. you want to print a nice table to stdout...

Sign up to request clarification or add additional context in comments.

Comments

1

To print the above data in a table I would suggest a simple loop with some string formating:

print "\t".join(['year', 'data', 'Jan', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEZ'])
theYearOffset = 1850
for theYearCounter in range(len(data1_yearly)):
    print "%s\t%s\t%s\n\t%s\t%s" % ((theYearOffset + theYearCounter), 
        'data1', "\t".join(["%.2f" % theValue for theValue in data1_yearly[theYearCounter]]), 
        'data2', "\t".join(["%.2f" % theValue for theValue in data2_yearly[theYearCounter]]))

This is not the most beautiful code possible but it will do the job. The columns are separated with tabulators and floating point numbers are roundet to 2 digits.

Here is the output for some stupid test data:

output

Test data:

data1 = [1.1233,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11]
data2 = [8,9,10,11,12,13,14,15,1,2,4,8,9,10,11,12,13,14,15,1,2,4,8,9,10,11,12,13,14,15,1,2,4,8,9,10,11,12,13,14,15,1,2,4]

2 Comments

Welcome to the python world. Python doas a lot syntactic suggar (you allready used list comprehension). You may take a look at stackoverflow.com/questions/101268 for some nice features of this language
Ya thanks for the information. i am really a beginner. will catch up surely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.