Home > Net >  first argument must be an iterable of pandas objects, you passed an object of type "Series"
first argument must be an iterable of pandas objects, you passed an object of type "Series"

Time:11-29

I have the following dataframe:

s = df.head().to_dict()
print(s)

{'BoP transfers': {1998: 12.346282212735618,
  1999: 19.06438060024298,
  2000: 18.24888031473687,
  2001: 24.860019912667006,
  2002: 32.38242225822908},
 'Current balance': {1998: -6.7953,
  1999: -2.9895,
  2000: -3.9694,
  2001: 1.1716,
  2002: 5.7433},
 'Domestic demand': {1998: 106.8610389799729,
  1999: 104.70302507466538,
  2000: 104.59254229534136,
  2001: 103.83532232336977,
  2002: 102.81709401489702},
 'Effective exchange rate': {1998: 88.134,
  1999: 95.6425,
  2000: 99.927725,
  2001: 101.92745,
  2002: 107.85565},
 'RoR (foreign liabilities)': {1998: 0.0433,
  1999: 0.0437,
  2000: 0.0542,
  2001: 0.0539,
  2002: 0.0474}}

which can be transformed back to its original form using

df = pd.DataFrame.from_dict(s)

I want to slice this dataframe in the following manner:

df_1 = df.iloc[:,0:2]
df_2 = pd.concat(df.iloc[:,0], df.iloc[:,3:])

when I get the titled error. I know there are some questions regarding this already, but I am unable to put the pieces together. Specifically, in my case, the dataframe is not this small (it has 100 columns). I want something along the lines of

df_1 = df.iloc[:,0:10]
df_2 = pd.concat(df.iloc[:,0], df.iloc[:,11:20])
df_3 = pd.concat(df.iloc[:,0], df.iloc[:,21:30])

and so on. How can this be accomplished? Thank you.

CodePudding user response:

You need to use a list of the DataFrames to merge and to concat on axis=1:

df_2 = pd.concat([df.iloc[:,0], df.iloc[:,3:]], axis=1)

Or, better, use slicing:

df_2 = df.iloc[:, [0,3,4]]
# or
df_2 = df.iloc[:, np.r_[0,3:df.shape[1]]]

Output:

      BoP transfers  Effective exchange rate  RoR (foreign liabilities)
1998      12.346282                88.134000                     0.0433
1999      19.064381                95.642500                     0.0437
2000      18.248880                99.927725                     0.0542
2001      24.860020               101.927450                     0.0539
2002      32.382422               107.855650                     0.0474
  • Related