[英]Expanding pandas dataframe with column range
I have a pandas dataframe with column range and strings similar to this:我有一个 Pandas 数据框,其列范围和字符串与此类似:
STREET LOWADD HIGHADD POSTAL SECTOR
0 ABBERLY CIR 1900 2000 23112 A6
1 ABBEY VILLAGE CIR 500 600 23114 B6
I need to expand/transform it to the below, between the LOWADD and HIGHADD columns and forward filling the data in STREET, POSTAL and SECTOR:我需要在 LOWADD 和 HIGHADD 列之间将其扩展/转换为以下内容,并向前填充 STREET、POSTAL 和 SECTOR 中的数据:
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
Whats the best way to do this with pandas?用熊猫做到这一点的最佳方法是什么?
Idea is subtract columns for number of repeated rows by Series.sub
, then repeat by Index.repeat
and DataFrame.loc
and last add counter Series by GroupBy.cumcount
to Street
column:想法是按Series.sub
减去重复行数的列,然后按Index.repeat
和DataFrame.loc
重复,最后按GroupBy.cumcount
添加计数器 Series 到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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.