Olive
Olive

Reputation: 644

How to rename a column while merging in pandas

I am using a for loop to merge many different dataframes. Each dataframe contains values from a specific time period. As such the column in each df is named "balance". In order to avoid creating multiple balance_x, balance_y... I want to name the columns using the name of the df.

so far, I have the following

top = topaccount_2021_12
top = top.rename(columns={"balance": "topaccount_2021_12"})
for i in [topaccount_2021_09, topaccount_2021_06, topaccount_2021_03,
          topaccount_2020_12, topaccount_2020_09, topaccount_2020_06, topaccount_2020_03,
          topaccount_2019_12, topaccount_2019_09, topaccount_2019_06, topaccount_2019_03,
          topaccount_2018_12, topaccount_2018_09, topaccount_2018_06, topaccount_2018_03,
          topaccount_2017_12, topaccount_2017_09, topaccount_2017_06, topaccount_2017_03,
          topaccount_2016_12, topaccount_2016_09, topaccount_2016_06, topaccount_2016_03,
          topaccount_2015_12, topaccount_2015_09]:
    top = top.merge(i, on='address', how='left')
    top = top.rename(columns={'balance': i})

But i get the error msg:

TypeError: Cannot convert bool to numpy.ndarray

Any idea how to solve this? Thanks!

Upvotes: 0

Views: 899

Answers (1)

dimas krisrianto
dimas krisrianto

Reputation: 188

I assume topaccount_* is a dataframe. I'm a bit confused in top = top.rename(columns={'balance': i}) because what do you want to achieve here? rename function used to rename column given key as original column name and value as the renamed column name. but instead of giving a string, you give dataframe to column

Edit

# store in dictionary
dictOfDf = {
    'topaccount_2021_09':topaccount_2021_09,
    'topaccount_2021_06':topaccount_2021_06,
    ...
    'topaccount_2015_09':topaccount_2015_09,
}

# pick the first dict to declare dataframe
top = dictOfDf[dictOfDf.keys()[0]]
top = top.rename(columns={"balance": dictOfDf.keys()[0]})

# iterate through all the keys
for i in dictOfDf.keys()[1:]:
    top = top.merge(i, on='address', how='left')
    top = top.rename(columns={'balance': i})

Upvotes: 1

Related Questions