I am worried about how to process data using Python's Pandas.
Can someone please teach me?
I would like to process the following data.
Use this data
import pandas as pd
pd.DataFrame([1, 'ame', 30, 'hare', 40], [2, 'kumori', 20, 'kosame', 30]], columns=['id', 'today_tenki', 'today_ondo', 'tomorrow_ondo')
I'd like to convert it to this kind of data.
I thought I could do this with pandas melt or stack, but it didn't work.
The following is a case of using numpy.containate.
>>import pandas as pd
>>import numpy as np
>>>df=pd.DataFrame([
[1, 'ame', 30, 'hare', 40],
[2, 'kumori', 20, 'kosame', 30]
], columns=['id', 'today_tenki', 'today_ondo', 'tomorrow_tenki', 'tomorrow_ondo')
>>df
id today_tenki today_ondo tomorrow_tenki tomorrow_ondo
0 1ame 30hare40
12 kumori 20 kosame 30
>>>dfn=pd.DataFrame(
np.concatenate((
df[['id', 'today_tenki', 'today_ondo']].values,
df[['id', 'tomorrow_tenki', 'tomorrow_ondo']].values
)), columns=('id', 'tenki', 'ondo')
.sort_values('id', ignore_index=True)
>>>dfn
id tenkiondo
01ame30
11hare40
22 kumori 20
32 kosame 30
We divided it into two data frames, renamed the column, combined and sorted.
Smarter might be the way.
import pandas as pd
df=pd.DataFrame([1, 'ame', 30, 'hare', 40], [2, 'kumori', 20, 'kosame', 30]], columns=['id', 'today_tenki', 'today_ondo', 'tomorrow_ondo')
df1 = df [['id', 'today_tenki', 'today_ondo']]]
df2 = df [['id', 'tomorrow_tenki', 'tomorrow_ondo']]
df2.columns = df1.columns
new_df=pd.concat([df1,df2], ignore_index=True).sort_values(by='id')
new_df.columns=['id', 'tenki', 'ondo']
© 2025 OneMinuteCode. All rights reserved.