Pandas: create dataframe from list of namedtuple

2024/11/19 1:18:28

I'm new to pandas, therefore perhaps I'm asking a very stupid question. Normally initialization of data frame in pandas would be column-wise, where I put in dict with key of column names and values of list-like object with same length.

But I would love to initialize row-wise without dynamically concat-ing rows. Say I have a list of namedtuple, is there a optimized operation that will give me a pandas data frame directly from it?

Answer

In a similar vein to creating a Series from a namedtuple, you can use the _fields attribute:

In [11]: Point = namedtuple('Point', ['x', 'y'])In [12]: points = [Point(1, 2), Point(3, 4)]In [13]: pd.DataFrame(points, columns=Point._fields)
Out[13]: x  y
0  1  2
1  3  4

Assuming they are all of the same type, in this example all Points.

https://en.xdnf.cn/q/26499.html

Related Q&A

Closest equivalent of a factor variable in Python Pandas

What is the closest equivalent to an R Factor variable in Python pandas?

Temporarily Disabling Django Caching

How do you disable Django caching on a per checkout basis?Back before Django 1.3, I could disable caching for my local development checkout by specifying CACHE_BACKEND = None, in a settings_local.py i…

How to get a complete exception stack trace in Python

The following snippet:import tracebackdef a():b()def b():try:c()except:traceback.print_exc()def c():assert Falsea()Produces this output:Traceback (most recent call last):File "test.py", line …

python - should I use static methods or top-level functions

I come from a Java background and Im new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing files. Some functions associated …

Draw graph in NetworkX

Im trying to draw any graph in NetworkX, but get nothing, not even errors:import networkx as nx import matplotlib.pyplot as plt g1=nx.petersen_graph() nx.draw(g1)

Django 1.7 migrations wont recreate a dropped table, why?

Using Django 1.7 migrations.I accidentally dropped a table in my database. I assumed that by running migration again this would recreate the table but no, Django states "No migrations to apply&quo…

Reset ipython kernel

I was wondering if there is a way to restart the ipython kernel without closing it, like the kernel restart function that exists in the notebook. I tried %reset but that doesnt seem to clear the import…

What is the best way to remove a dictionary item by value in python? [duplicate]

This question already has answers here:Removing entries from a dictionary based on values(4 answers)Closed 4 years ago.I wonder if there is simple way to remove one or more dictionary element(s) from a…

How do I run a Python script on my web server?

Ive just started learning Python, and Im pretty lost right now. I want to run my script on my server that is hosted through hosting24.com. Their FAQ says they support Python, but I have no clue where t…

How to create an OrderedDict in Python?

I tried to maintain the order of a Python dictionary, since native dict doesnt have any order to it. Many answers in SE suggested using OrderedDict.from collections import OrderedDictdomain1 = { "…