Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用列范围扩展数据帧_Python_Pandas - Fatal编程技术网

Python 使用列范围扩展数据帧

Python 使用列范围扩展数据帧,python,pandas,Python,Pandas,我有一个数据框,其列范围和字符串类似于: STREET LOWADD HIGHADD POSTAL SECTOR 0 ABBERLY CIR 1900 2000 23112 A6 1 ABBEY VILLAGE CIR 500 600 23114 B6 我需要在LOWADD和HIGHADD列之间将其展开/转换到下面,并向前填充街道、邮政和部门中的数据: New_Street

我有一个数据框,其列范围和字符串类似于:

     STREET             LOWADD  HIGHADD POSTAL  SECTOR
0   ABBERLY CIR         1900    2000    23112   A6
1   ABBEY VILLAGE CIR   500     600     23114   B6
我需要在LOWADD和HIGHADD列之间将其展开/转换到下面,并向前填充街道、邮政和部门中的数据:

New_Street              POSTAL  SECTOR
1901 ABBERLY CIR        23112   A6
1902 ABBERLY CIR        23112   A6
1903 ABBERLY CIR        23112   A6
1904 ABBERLY CIR        23112   A6
1905 ABBERLY CIR        23112   A6

使用pandas执行此操作的最佳方法是什么?

想法是将列减去重复行的数量,然后重复和,最后将计数器序列添加到
Street
列:

df = df.reset_index(drop=True)
diff = df['HIGHADD'].sub(df['LOWADD'])
df = df.loc[df.index.repeat(diff)]
s = df.groupby(level=0).cumcount().add(1).add(df['LOWADD']).astype(str)
df['STREET'] = s + ' ' + df['STREET']
df = df.drop(['LOWADD','HIGHADD'], axis=1).reset_index(drop=True)
print (df)
                    STREET  POSTAL SECTOR
0         1901 ABBERLY CIR   23112     A6
1         1902 ABBERLY CIR   23112     A6
2         1903 ABBERLY CIR   23112     A6
3         1904 ABBERLY CIR   23112     A6
4         1905 ABBERLY CIR   23112     A6
..                     ...     ...    ...
195  596 ABBEY VILLAGE CIR   23114     B6
196  597 ABBEY VILLAGE CIR   23114     B6
197  598 ABBEY VILLAGE CIR   23114     B6
198  599 ABBEY VILLAGE CIR   23114     B6
199  600 ABBEY VILLAGE CIR   23114     B6

[200 rows x 3 columns]

你有没有试过什么出错的地方,想在这里分享?