在这两种形式之间转换有好的现有方法吗?
+--------+-------------+
| FRUIT | ATTRIBUTES |
+--------+-------------+
| banana | long|yellow |
+--------+-------------+
| kiwi | brown|oval |
+--------+-------------+
和
+--------+-----------+
| FRUIT | ATTRIBUTE |
+--------+-----------+
| banana | long |
+--------+-----------+
| banana | yellow |
+--------+-----------+
| kiwi | brown |
+--------+-----------+
| kiwi | oval |
+--------+-----------+
我目前正在迭代解包过程的行,我听说不鼓励这样做。
import pandas as pd
packed = pd.DataFrame([['banana', 'long|yellow'],
['kiwi', 'brown|oval']],
columns=['FRUIT', 'ATTRIBUTES'])
pack_delim = '|'
per_fruit_frames = []
for row in packed.itertuples(index=True, name='Pandas'):
row_attribs = row.ATTRIBUTES
row_attribs_split = row_attribs.split(pack_delim)
row_attribs_series = pd.Series(row_attribs_split)
ras_len = len(row_attribs_split)
fruit_rep = [row[1]] * ras_len
frs = pd.Series(fruit_rep)
temp = pd.concat([frs, row_attribs_series], axis=1)
per_fruit_frames.append(temp)
unpacked = pd.concat(per_fruit_frames)
unpacked.columns = packed.columns
2条答案
按热度按时间ffdz8vbo1#
尝试:
或
分两步进行:
产量
packed
:vsdwdz232#
另一种方式: