0

I have the following;

mylist = ["name", "state", "name", "city"]
newlist = ["name1", "state", "name2", "city"]

I have renamed the duplicates. I would like to merge the name1 and name2 and rename to name

wwnde
  • 14,189
  • 2
  • 8
  • 21
user1713716
  • 35
  • 1
  • 4
  • Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – wwnde May 04 '20 at 01:59
  • Can you provide a small snippet of your input dataframe and expected output? – Scott Boston May 04 '20 at 02:26
  • I have column names ["name", "state", "name", "city"] and I have renamed to ["name1", "state", "name2", "city"]. I want to merges name1 and name 2 and then rename to Name. Hope I am clear. Thanks – user1713716 May 04 '20 at 03:09

1 Answers1

0

This method works well to do this and is clean:

Renamed DataFrame method accepts the dictionary "key: values" that allow you to map the old value to the new value.

Example.

new_df = "read in data source"

col_map = {"name": "name1",
    "state": "state",
    "name": "name2",
    "city": "city"
}

new_df.rename(columns=col_map)

This method should work perfectly fine. Please respond so that our team here in StackOverflow knows that you care about our feedback and are responsive.

Alfred Hull
  • 109
  • 8