[Python] How to flatten column headers in a Python geopandas.geodataframe.GeoDataFrame object?

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    when I run nodesA.head(), I can see this object has a multi-dimensional hierarchy of headings. I have tried to flatted it by using drop level and flatten as shown in the commented out lines. How can I remove the multidimensional nature of the headings?

    import osmnx as ox
    import pandas as pd
    G_tester = ox.graph_from_place("New York, New York, USA", network_type="drive")

    nodesA = ox.graph_to_gdfs(G_tester)[0]
    #print(type(nodesA))
    #print(nodesA)
    # nodesA['osmid']
    nodesA.flatten(().head())
    #nodesA.columns.droplevel()
    #nodesA.index = nodesA.index.droplevel(2)
    #print(nodesA.index)

    Continue reading...

Compartilhe esta Página