Home > Enterprise >  Consolidate column values into one column as a list with column name as key in python
Consolidate column values into one column as a list with column name as key in python

Time:08-03

I have data frame that looks like this:

id  address city    pincode
1   here is address 1   city1   1234
2   here is address 2   city2   4321
3   here is address 3   city3   7654
4   here is address 4   city4   9876
5   here is address 5   city5   987

What I am trying to achieve is:

id  address city    pincode Newcolumn
1   here is address 1   city1   1234    address:'here is address 1', city: 'city1', pincode:'1234'
2   here is address 2   city2   4321    address:'here is address 2', city: 'city2', pincode:'4321'
3   here is address 3   city3   7654    address:'here is address 3', city: 'city3', pincode:'7654'
4   here is address 4   city4   9876    address:'here is address 4', city: 'city4', pincode:'9876'
5   here is address 5   city5   987 address:'here is address 5', city: 'city5', pincode:'987'

I have been trying to do this:

cols = df[['address', 'city', 'pincode']]

df['Newcolumn'] = df[cols].str.join()

CodePudding user response:

Can be accomplished with to_dict(orient='records'):

import pandas as pd
from io import StringIO

# create dataframe
data = StringIO("""id;address;city;pincode
1;here is address 1;city1;1234
2;here is address 2;city2;4321
3;here is address 3;city3;7654
4;here is address 4;city4;9876
5;here is address 5;city5;987
""")
df = pd.read_csv(data, sep=';', index_col='id')

# assign new column
df = df.assign(Newcolumn = df.to_dict(orient='records'))

Output:

DataFrame

CodePudding user response:

You can use apply function

df['Newcolumn'] = df.apply(lambda x: f" Address: {x.address}, City: {x.city}, Pincode: {x.pincode}", axis=1)

The output:

enter image description here

  • Related