DataFrame types of data include:
We would like to keep the contents of the data in a single column of the Pandas DataFrame type (fish, meet, vegetable).
date member total fish meet vegetable
October 1, 2020 SATO 2000 600 600 400
November 1, 2020 KATO 1500 600 400 400
December 1, 2020 MITO 1800 600 600 600 600 600
I would like to leave the date, member, total in the column and put the three columns fish, meet, and vegetable together to create a new column called [Grocery], but it doesn't work even if I try stack etc.
Please let me know if there is a good way.
The Python version is 3.9.1.
python pandas
Retain data content
Here are some examples of how to:
Also, stack is a matrix replacement, so it may be different from the purpose you are looking for this time.
sample code
import pandas as pd
df = pd.DataFrame({
'date': ['2020/10/1', '2020/11/1', '2020/12/1',]
'member': ['SATO', 'KATO', 'MITO',
'total': [2000, 1500, 1800],
'fish': [600, 600, 600],
'meet': [600, 400, 400],
'vegetable': [400,400,600]
})
# 1. Sum up the values of multiple columns
df['Grocery'] = df['fish'] + df['meet'] + df['vegetable']
# 2. Concatenate multiple columns as hyphen-separated strings
df['Grocery2'] = df['fish'].asttype(str).str.cat([df['meet'].asttype(str), df['vegetable'].asttype(str), sep='-')
# 3. Keep multiple columns as an array
def to_list(x):
return [x.loc ['fish'], x.loc ['meet'], x.loc ['vegetable']]
df['Grocery3'] = df.apply(to_list,axis=1)
print(df)
Run Results
date member total fish meet vegetable Grocery Grocery 2 Grocery 3
0 2020/10/1 SATO 2000 600 600 400 1600 600-600-400 [600,600,400]
1 November 1, 2020 KATO 1500 600 400 400 1400 600-400 [600, 400, 400]
2 December 1, 2020 MITO 1800 600 400 600 1600 600-400-600 [600, 400, 600]
df['Grocery'] = df[['fish', 'meet', 'vegetable']].values.tolist()
print(df)
#
date member total fish meet vegetable Grocery
0 2020/10/1 SATO 2000 600 600 400 [600,600,400]
1 November 1, 2020 KATO 1500 600 400 400 [600, 400, 400]
2 December 1, 2020 MITO 1800 600 400 600 [600, 400, 600]
df['Grocery'] = df[['fish', 'meet', 'vegetable']].to_dict('records')
#
date member total fish meet vegetable Grocery
0 2020/10/1 SATO2000 600 600 400 {'fish':600, 'meet':600, 'vegetable':400}
1 November 1, 2020 KATO 1500 600 400 {'fish':600, 'meet':400, 'vegetable':400}
2 December 1, 2020 MITO 1800 600 400 600 {'fish':600, 'meet':400, 'vegetable':600}
.str
is an instance of the pandas.core.strings.accessor.StringMethods
class (accessory methods as the name suggests), so you can access each element as follows:
# For the list:
print(df['Grocery'].str[0])
#
0 600
1 600
2 600
Name:Grocery, dtype:int64
# in the case of a dictionary
print(df['Grocery'].str['vegetable'])
0 400
1 400
2 600
Name:Grocery, dtype:int64
© 2025 OneMinuteCode. All rights reserved.