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 |
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.