两行带分隔符号的列,转换成多行
地市 | id | 挂高 | 方位角 |
三国 | id_1 | 32\32\22\32 | 50\150\300 |
三国 | id_2 | 25\25\25 | 70\230\350 |
三国 | id_3 | 41\41\41 | 5\110\200 |
三国 | id_4 | 35\35\35 | 90\170\270 |
三国 | id_5 | 28\28\28 | 55\200\330 |
三国 | id_6 | 30\27\27 | 60\195\310 |
三国 | id_7 | 31\31\31 | 0\120\240 |
三国 | id_8 | 25\25\25 | 30\240\233\315 |
第一种方法:导入上表数据为data_B 导出data_res
data_z = pd.DataFrame()
for name,data_1,data_2 in zip(data_B['新规划站号'],data_B['挂高'],data_B['方位角']):
try:
data_t = pd.concat([pd.DataFrame(data_1.split('/'),columns=['挂高']),pd.DataFrame(data_2.split('/'),columns=['方位角'])],axis=1)
except:
data_t = pd.concat([pd.DataFrame([data_1],columns=['挂高']),pd.DataFrame([data_2],columns=['方位角'])],axis=1)
data_t['新规划站号'] =name
data_z = data_z.append(data_t)
第二中方法:导入上表数据为data 导出df
t_list = []
for idx,row in data.iterrows():
heighs = row['挂高'].split('\\')
amzs = row['方位角'].split('\\')
if len(heighs) == len(amzs):
new_list = map(list,zip(heighs,amzs))
elif len(heighs) > len(amzs):
for i in range(len(heighs)-len(amzs)):
amzs.append(' ')
new_list = map(list,zip(heighs,amzs))
elif len(heighs) < len(amzs):
for i in range(len(amzs)-len(heighs)):
heighs.append(' ')
new_list = map(list,zip(heighs,amzs))
for l in new_list:
cols = [str(row['地市']),str(row['id'])]
cols.extend(l)
t_list.append(cols)
df = pd.DataFrame(t_list,columns = ['地市','id','挂高','方位角'])
dft_list = []
for idx,row in data.iterrows():
heighs = row['挂高'].split('\\')
amzs = row['方位角'].split('\\')
if len(heighs) > len(amzs):
for i in range(len(heighs)-len(amzs)):
amzs.append(' ')
elif len(heighs) < len(amzs):
for i in range(len(amzs)-len(heighs)):
heighs.append(' ')
new_list = map(list,zip(heighs,amzs))
for l in new_list:
cols = [str(row['地市']),str(row['id'])]
cols.extend(l)
t_list.append(cols)
df = pd.DataFrame(t_list,columns = ['地市','id','挂高','方位角'])