Nijan
Nijan

Reputation: 638

Table-like data structure in python

Is there a data-structure in Python that approximate the behavior of a database table?

My data looks like the following:

id | name | description

1 | test | this is a test

The closest I can get is to have a list of dictionaries, but I was wondering if there exists a specific data-structure for that purpose (that, for example, supports sorting or add).

Upvotes: 3

Views: 7364

Answers (4)

Robert Reinhold
Robert Reinhold

Reputation: 21

Pandas is a python framework, which works with dataframes for data analysis. Look here:

http://pandas.pydata.org/

Example:

# Import libraries
import pandas as pd
import sys
data = {[name:"test", ...], description:["this is a test", ...]}

df = pd.DataFrame(data)
df

Should give this:

  name description
0 test this is a test

Upvotes: 1

Thomas Dussaut
Thomas Dussaut

Reputation: 746

Pandas is what you are looking for.

You can generate DataFrames from array-like types, like list of dict, dict of list, list of list, np.Array...

Example :

import pandas as pd
df=pd.DataFrame({'wololo':[1,2,3,4,5,6],'text':['yay','toto','tat','tata','billy','bill']})

The output will look like this

    text  wololo
0    yay       1
1   toto       2
2    tat       3
3   tata       4
4  billy       5
5   bill       6

It also have useful I/O Tools to read a lot of different files such as Excel, CSV, JSON, etc...

Check also all the DataFrame methods to manipulate them in the more efficient way possible.

Upvotes: 2

ttous
ttous

Reputation: 406

I have worked on an ordered Python dictionnary. It seems like that's what you're looking for. You can find it here.

It supports :

get(key)
del(key)
set(key, value)
sort()
contains(key)
reverse()
add(another_dictionary)

Upvotes: 0

Sudarshan shenoy
Sudarshan shenoy

Reputation: 27

In dictionary you can sort by names and you can also create lists inside dictionaries which can do all the operations you are looking for!! There is also something like Ordered Dictionary which can help you!! https://docs.python.org/2/library/collections.html

Upvotes: 0

Related Questions