How to turn pandas dataframe row into ordereddict fast

user1246428

Looking for a fast way to get a row in a pandas dataframe into a ordered dict with out using list. List are fine but with large data sets will take to long. I am using fiona GIS reader and the rows are ordereddicts with the schema giving the data type. I use pandas to join data. I many cases the rows will have different types so I was thinking turning into a numpy array with type string might do the trick.

Andy Hayden

Unfortunately you can't just do an apply (since it fits it back to a DataFrame):

In [1]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])

In [2]: df
Out[2]: 
   a  b
0  1  2
1  3  4

In [3]: from collections import OrderedDict

In [4]: df.apply(OrderedDict)
Out[4]: 
   a  b
0  1  2
1  3  4

But you can use a list comprehension with iterrows:

In [5]: [OrderedDict(row) for i, row in df.iterrows()]
Out[5]: [OrderedDict([('a', 1), ('b', 2)]), OrderedDict([('a', 3), ('b', 4)])]

If it was possible to use a generator, rather than a list, to whatever you were working with this will usually be more efficient:

In [6]: (OrderedDict(row) for i, row in df.iterrows())
Out[6]: <generator object <genexpr> at 0x10466da50>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to turn pandas dataframe row into ordereddict fast

From Dev

Pandas dataframe: turn date in column into value in row

From Dev

Pandas dataframe: turn date in column into value in row

From Dev

Generate a pandas dataframe from ordereddict?

From Dev

How do I turn pandas DataFrame groupby results into a DataFrame?

From Dev

How to turn tuple of tuples in unicode to pandas dataframe in python

From Dev

How do I turn an array of column names into a pandas Dataframe?

From Dev

How to turn a Pandas Dataframe into a Dictionary of Dataframes by grouping columns

From Dev

How to turn tuple of tuples in unicode to pandas dataframe in python

From Dev

How to add an extra row to a pandas dataframe

From Java

How to add row name to cell in pandas dataframe?

From Java

How to add header row to a pandas DataFrame

From Java

How to get previous row with condition in a DataFrame of Pandas

From Dev

How to set row value in pandas dataframe?

From Dev

How to insert a row in a Pandas multiindex dataframe?

From Dev

How to get row delta value by pandas dataframe

From Dev

How to drop a row from Pandas dataframe?

From Dev

How to add conditional row to pandas dataframe

From Dev

How to print a specific row of a pandas DataFrame?

From Dev

How to get row number in dataframe in Pandas?

From Dev

How to reset row's value in Pandas for a dataframe?

From Dev

How to get row delta value by pandas dataframe

From Dev

How to remove ugly row in pandas.dataframe

From Dev

How to drop a row from Pandas dataframe?

From Dev

How to drop designate row of pandas dataframe

From Dev

How to continuous assign a variable to a dataframe row (pandas)?

From Dev

How to reverse the order of a row in a pandas dataframe

From Dev

How to turn a generic dataframe?

From Dev

Python Pandas: How to move one row to the first row of a Dataframe?

Related Related

  1. 1

    How to turn pandas dataframe row into ordereddict fast

  2. 2

    Pandas dataframe: turn date in column into value in row

  3. 3

    Pandas dataframe: turn date in column into value in row

  4. 4

    Generate a pandas dataframe from ordereddict?

  5. 5

    How do I turn pandas DataFrame groupby results into a DataFrame?

  6. 6

    How to turn tuple of tuples in unicode to pandas dataframe in python

  7. 7

    How do I turn an array of column names into a pandas Dataframe?

  8. 8

    How to turn a Pandas Dataframe into a Dictionary of Dataframes by grouping columns

  9. 9

    How to turn tuple of tuples in unicode to pandas dataframe in python

  10. 10

    How to add an extra row to a pandas dataframe

  11. 11

    How to add row name to cell in pandas dataframe?

  12. 12

    How to add header row to a pandas DataFrame

  13. 13

    How to get previous row with condition in a DataFrame of Pandas

  14. 14

    How to set row value in pandas dataframe?

  15. 15

    How to insert a row in a Pandas multiindex dataframe?

  16. 16

    How to get row delta value by pandas dataframe

  17. 17

    How to drop a row from Pandas dataframe?

  18. 18

    How to add conditional row to pandas dataframe

  19. 19

    How to print a specific row of a pandas DataFrame?

  20. 20

    How to get row number in dataframe in Pandas?

  21. 21

    How to reset row's value in Pandas for a dataframe?

  22. 22

    How to get row delta value by pandas dataframe

  23. 23

    How to remove ugly row in pandas.dataframe

  24. 24

    How to drop a row from Pandas dataframe?

  25. 25

    How to drop designate row of pandas dataframe

  26. 26

    How to continuous assign a variable to a dataframe row (pandas)?

  27. 27

    How to reverse the order of a row in a pandas dataframe

  28. 28

    How to turn a generic dataframe?

  29. 29

    Python Pandas: How to move one row to the first row of a Dataframe?

HotTag

Archive