首页 > 解决方案 > 如何在python pandas中将值范围更改为实际值

问题描述

我在 pandas 数据框中有数据,其中列值看起来像 732-738 我想将这些值范围扩展到单独的行中,这样做也可以扩展下一列值,任何人都可以建议我这样做吗?

 Destination ZIP    Zone    
  732-738             8 
   005                7

标签: pythonpandasstringdataframedata-cleaning

解决方案


这将完成这项工作

df['Destination']=df['Destination'].apply(lambda x:  range([int(i) for i in x.split('-')][0],[int(i) for i in x.split('-')][1]+1) if "-" in x else x)

df.explode('Destination')


  Destination_ZIP Zone
0             732    8
0             733    8
0             734    8
0             735    8
0             736    8
0             737    8
0             738    8
1             005    7

推荐阅读