3

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).

2

4 Answers 4

2

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.

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

Comments

1

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

1 Comment

Please flesh this out and give an example, you'll get my vote.
0

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

Comments

0

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)

1 Comment

The problem with ordered dictionaries is that you can only have one key:value pair. For example, I'd like all my rows in the table to have a key 'id' and a value for that ID.

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.