0

I want use the first 5 rows of my data frame and concentrate the string to form a new index.

Doing some research i think groupby with agg and lambda will work. What would be the best way to accomplish this? I am new to python.

For example my current data frame:

Dataframe (df):

a b c d e f
fixed variable electric
Cost Cost Power Max Min
receipt Unit Unit Another header Rate Rate
delivery zone Rate Rate Rate 1 1/2
$ $ $ $ $
5 2 3 3 3 3
5 2 3 3 3 3

Desired Outcome:

receipt delivery zone fixed cost unit rate variable cost unit rate electric Power Another header Rate Max Rate Min Rate
5 2 3 3 3 3
5 2 3 3 3 3

1 Answer 1

1

you don't need to use .groupby() or lambda. Simply use .agg(sum) on the first n-th rows to get concatenate the strings.

first step, get the slice:

slc = df.iloc[:4,:]

use .agg(sum) to aggregate the strings in one row.

new_cols = slc.agg(sum)

Simply reassign the column names

df.columns = new_cols
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.