本帖最后由 zxc9989 于 2022-4-19 10:26 编辑
一:[Python] 纯文本查看 复制代码 import pandas as pd
data = pd.DataFrame({'price':[2.1,2.2,2.3,1.8], 'quantity':[2,1,3,1]})
new_data = data['price'].repeat(data['quantity'])
new_data.index = range(1, new_data.shape[0]+1)
print(new_data)
二(生成器):
[Python] 纯文本查看 复制代码 import pandas as pd
def repeat(data:pd.DataFrame):
for item in data.iterrows():
yield from (item[1]['price'] for _ in range(int(item[1]['quantity'])))
data = pd.DataFrame({'price':[2.1,2.2,2.3,1.8], 'quantity':[2,1,3,1]})
data = pd.Series(repeat(data))
print(data) |