我有多个列,列中有多个值,用","分隔。例如:restaurant_type列具有类似'Casual Dining'以及'Cafe, Casual Dining'的值。由于存在87唯一值,因此在可视化时会出现问题。
","
restaurant_type
'Casual Dining'
'Cafe, Casual Dining'
87
k7fdbhmy1#
https://www.pythonpool.com/python-code-to-convert-a-table-to-first-normal-form/ import pandas as pd import numpy as np df = pd.DataFrame({'Name': ['David', 'Glenn', 'Steve'], 'Subjects': [ ['English', 'Math'], ['Math'], ['Science', 'English']]}) print(df) lens = list(map(len, df['Subjects'].values)) res = pd.DataFrame({'Name': np.repeat( df['Name'], lens), 'Subject': np.concatenate(df['Subjects'].values)}) print(res)
1条答案
按热度按时间k7fdbhmy1#