Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Adding Tables and Extracting Values from Tables
#1
Hi there

I would like to know how to write Python code to add tables and extract values from tables which have more than two columns?
An example table is shown below:

Parameter_A|Parameter_B
___________|__1|__2|__3
2__________|_50|100|150
4__________|100|150|200
6__________|150|200|250
8__________|200|250|300

For example, when parameter A is 2, 4, 6 or 8, and parameter B is 1, 2 or 3, then what is the code to extract values from the table above?

Thanks in advance

James Cox
Quote
#2
Here's how you create a database with sqlite.
import sqlite3 # you need this import at the beginning of the code.

conn = sqlite3.connect('example.db') 
cursor = conn.cursor()
sql = '''create table example (
         name1,
         name2,
         name3)'''
cursor.execute(sql)
cursor.close()
to see the table.
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
sql = '''select * from example'''
results = cursor.execute(sql)
somevariable = results.fetchall()
for var in somevariable:
    print(var)
Quote
#3
Hi mcmxl22

Thank you for your answer, although I don't know if the code you gave me is the answer to my query.

James
Quote
#4
I have been advised that the answer to my own query is:

Nested list in a dictionary should do the trick. You could also use a matrix.

For example

lookup = {2:[50,100,150],4:[100,150,200],6:[150,200,250],8:[200,250,300]}

So if you want to get Parameter B value 2 of parameter A=6 then you can do

print(lookup[6][1])

This looks up the dictionary 'lookup' for key 6 which contains [150,200,250]. Then it returns index 1 (the second value) of that list =200.
Quote
#5
You could represent a table as a list of lists, or a dictionary of lists, or a numpy matrix, or a dataframe in pandas, or a class, or ... well, lots of options really. It depends on what you are trying to do and the source of the data you are dealing with.

I see you picked a dictionary. Keep in mind that the keys in a dictionary must be unique, so you cannot repeat rows/columns.
I am trying to help you, really, even if it doesn't always seem that way
Quote
#6
Thanks gruntfutuk,

I have read your message. Thanks for writing, "Keep in mind that the keys in a dictionary must be unique, so you cannot repeat rows/columns."

James
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas dataframe to join three tables using like condition among them sandeep_ganga 0 146 Nov-29-2019, 08:30 AM
Last Post: sandeep_ganga
  Bar Plot with Python ,matplotlib and sqlite3 tables gauravbhardwajee 0 1,022 Sep-25-2018, 06:17 PM
Last Post: gauravbhardwajee
  Match two tables to select exactly matching items klllmmm 1 906 Nov-25-2017, 05:42 PM
Last Post: heiner55
  Pandas df.read_html dropping duplicate tables in html : Urgent Guidance radioactive9 1 1,167 Aug-05-2017, 04:48 AM
Last Post: radioactive9

Forum Jump:


Users browsing this thread: 1 Guest(s)