1

I have DataFrame flows representing people crossing borders

flows = DataFrame([[1,2],[3,4]], index=['Monday', 'Tuesday'], columns=['CZ>DE', 'HU>AT'])

         CZ>DE  HU>AT
Monday       1      2
Tuesday      3      4

I would like to split each column in two columns representing country increment/decrement per border. My current code and desired outcome is this

country_from = lambda x: x[:2]
country_to = lambda x: x[3:]
flows_from = -1*flows.copy()
flows_from.columns = pd.MultiIndex.from_tuples([(border, country_from(border)) for border in flows.columns])
flows_to = flows.copy()
flows_to.columns = pd.MultiIndex.from_tuples([(border, country_to(border)) for border in flows.columns])
country_flows = pd.concat([flows_from, flows_to], axis=1)
country_flows = country_flows.groupby(level=[0,1], axis=1).sum()

           CZ>DE    HU>AT   
           CZ DE    AT HU
Monday     -1  1     2 -2
Tuesday    -3  3     4 -4 

This solution is quite verbose and I suspect it could be done better. Would somebody have an idea?

rahlf23
  • 8,869
  • 4
  • 24
  • 54
PyJan
  • 88
  • 5

2 Answers2

0

You can create tuples that define the levels of your MultiIndex:

tuples = [(i,k) for i, j in zip(flows.columns,[i.split('>') for i in flows.columns]) for k in j]

x = flows.values

Then:

data = np.multiply(np.tile([-1,1], x.shape), np.repeat(x, 2, axis=1))

pd.DataFrame(data=data, index=flows.index, columns=pd.MultiIndex.from_tuples(tuples))

Yields:

        CZ>DE    HU>AT   
           CZ DE    HU AT
Monday     -1  1    -2  2
Tuesday    -3  3    -4  4
rahlf23
  • 8,869
  • 4
  • 24
  • 54
0

Ok, after being inspired here python/pandas: how to combine two dataframes into one with hierarchical column index? I solved the issue by concatenating dictionary of DataFrames. Having my original mapping lambda functions

country_from = lambda x: x[:2]
country_to = lambda x: x[3:]

the result could be obtained on one line

pd.concat({col:pd.DataFrame({country_from(col):-1*flows[col], country_to(col):flows[col]}) for col in flows.columns}, axis=1)

           CZ>DE    HU>AT   
           CZ DE    AT HU
Monday     -1  1     2 -2
Tuesday    -3  3     4 -4
PyJan
  • 88
  • 5