I'm worried about how to process the Pandas table.

Asked 2 years ago, Updated 2 years ago, 338 views

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')

Enter a description of the image here

I'd like to convert it to this kind of data.

Enter a description of the image here
I thought I could do this with pandas melt or stack, but it didn't work.

python pandas

2022-09-30 21:51

2 Answers

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


2022-09-30 21:51

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']


2022-09-30 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.