Skip to content

When constructing DataFrame from list of Series use names for index. #1494

Closed
@lodagro

Description

@lodagro

From mailing list

When constructing a DataFrame from a list of series, use the series name as index value (if possible).
Workaround is to use DataFrame.from_items()

In [21]: series = [pandas.Series(np.random.rand(3), name=c) for c in list('abcdefg')]

In [22]: df = pandas.DataFrame(series)

In [23]: df
Out[23]:
          0         1         2
0  0.609676  0.023603  0.687154
1  0.060276  0.173545  0.216510
2  0.016216  0.078186  0.493883
3  0.648037  0.640642  0.652898
4  0.590155  0.132365  0.257735
5  0.590068  0.124617  0.104589
6  0.364781  0.473282  0.218554

In [24]: df = pandas.DataFrame.from_items([(s.name, s) for s in series]).T

In [25]: df
Out[25]:
          0         1         2
a  0.609676  0.023603  0.687154
b  0.060276  0.173545  0.216510
c  0.016216  0.078186  0.493883
d  0.648037  0.640642  0.652898
e  0.590155  0.132365  0.257735
f  0.590068  0.124617  0.104589
g  0.364781  0.473282  0.218554

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions