Python Pandas中的range()列
我有一个DataFrame,看起来像:Python Pandas中的range()列,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个DataFrame,看起来像: data_df = pd.DataFrame({ 'col1': ['a', 'b', 'c'], 'col2': [2, 1, 3] }) 目标是在col2上应用范围,并获得表示此数据的新数据帧: a 1 a 2 b 1 c 1 c 2 c 3 到目前为止,我已经有了两个数据帧: requests_series = pd.Series([np.arange(0, col2, 1) + 1 for col1, col2 in zip(data_
data_df = pd.DataFrame({
'col1': ['a', 'b', 'c'],
'col2': [2, 1, 3]
})
目标是在col2
上应用范围,并获得表示此数据的新数据帧:
a 1
a 2
b 1
c 1
c 2
c 3
到目前为止,我已经有了两个数据帧:
requests_series = pd.Series([np.arange(0, col2, 1) + 1 for col1, col2 in zip(data_df.col1, data_df.col2)], data_df.index, name='requests')
requests_df = requests_series.to_frame()
data_df.pop('col2')
现在,我一直在思考如何以理想的方式合并它们
我宁愿寻找一种有效的方法,因为在生产过程中,它最终将处理1000万行。让我们尝试
重复,然后groupby().cumcount()
:
输出:
col1 col2
0 a 1
0 a 2
1 b 1
2 c 1
2 c 2
2 c 3
谢谢,这就是我要找的!
col1 col2
0 a 1
0 a 2
1 b 1
2 c 1
2 c 2
2 c 3