Pandas Dataframe Growing In Rows And Columns
I have the following dataframe (which may grow in rows and Info columns): City Country Info1 Info2 BCN Spain 3 5.6 Moscow Russia 4 7 I'm trying t
Solution 1:
If you are okay with a small tweak in your output, you can use melt
and to_dict
directly to get separate dictionaries for each info:
>>> df.melt(['City', 'Country']).to_dict('r')
[{'City': 'BCN', 'Country': 'Spain', 'value': 3.0, 'variable': 'Info1'},
{'City': 'Moscow', 'Country': 'Russia', 'value': 4.0, 'variable': 'Info1'},
{'City': 'BCN', 'Country': 'Spain', 'value': 5.6, 'variable': 'Info2'},
{'City': 'Moscow', 'Country': 'Russia', 'value': 7.0, 'variable': 'Info2'}]
Solution 2:
For a non-Pandas-specific solution, this split_rows
function works for any iterable of namedtuples (or if you change the rd = ...
line, anything that can be dictified).
import pandas as pd
def split_rows(namedtuple_iterable, cols):
for row in namedtuple_iterable:
rd = row._asdict()
cs = [(col, rd.pop(col)) for col in cols]
for key, value in cs:
yield {**rd, key: value}
df = pd.DataFrame(
{
"city": ["BCN", "Moscow"],
"country": ["Spain", "Russia"],
"inf_1": [3, 5],
"inf_2": [4, 7],
}
)
for sr in split_rows(df.itertuples(), ("inf_1", "inf_2")):
print(sr)
outputs
{'Index': 0, 'city': 'BCN', 'country': 'Spain', 'inf_1': 3}
{'Index': 0, 'city': 'BCN', 'country': 'Spain', 'inf_2': 4}
{'Index': 1, 'city': 'Moscow', 'country': 'Russia', 'inf_1': 5}
{'Index': 1, 'city': 'Moscow', 'country': 'Russia', 'inf_2': 7}
Post a Comment for "Pandas Dataframe Growing In Rows And Columns"