[Solved] How to separate the contents of parentheses and make a new dataframe column? [closed]


Seems like str.extract would work assuming the seat number is the numeric characters before and the seat arrangement is the values inside the parenthesis:

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'seat': ['45席(1階カウンター4席、6〜8人テーブル1席2階地下それぞれ最大20人)',
             np.nan,
             np.nan,
             np.nan,
             '9席(カウンター9席、個室4席)']
})
new_df = df['seat'].str.extract(r'(\d+)席((.*))', expand=True)
new_df.columns = ['seat number', 'seat arrangement']

new_df:

  seat number                   seat arrangement
0          45  1階カウンター4席、6〜8人テーブル1席2階地下それぞれ最大20人
1         NaN                                NaN
2         NaN                                NaN
3         NaN                                NaN
4           9                       カウンター9席、個室4席

solved How to separate the contents of parentheses and make a new dataframe column? [closed]