我有一个有源到目的地流,看上去就像一个list
-
path_list = ['A', 'A', 'B', 'C', 'C']
我想填充上面的列表中qazxsw POI有3列 - qazxsw POI。例如 -
DataFrame
我想填充基于规则的source, destination, flag
列 - 如果source destination flag
'A' 'A' Type_1
'A' 'B' -
'B' 'C' -
'C' 'C' Type_2
第2项是相同的,然后flag
如果最后2项是相同的,然后list
。其他所有源 - 目的地对将被标记为Type_1
我到一半,并有填充Type_2
和-
列的脚本 -
source
如何添加的标志列,并填充它?
给特定的单元格的值使用destination
pd.DataFrame({'source': path_list[:-1], 'destination': path_list[1:]})
输出:
df.flag.iat[0]
数据帧从import pandas as pd
path_list = ['A', 'A', 'B', 'C', 'C']
df = pd.DataFrame({'source': path_list[:-1], 'destination': path_list[1:]})
df['flag'] = '-'
if path_list[0] == path_list[1]:
df.flag.iat[0] = 'Type_1'
if path_list[-1] == path_list[-2]:
df.flag.iat[-1] = 'Type_2'
print(df)
创建的,所以只能分配与第一和最后一个值和重复 source destination flag
0 A A Type_1
1 A B -
2 B C -
3 C C Type_2
新的列表:
path_list
但是,如果需要通过列表创建-
的第一2和最后2个值一致价值观和path_list = ['A', 'A', 'B', 'C', 'C']
df = pd.DataFrame({'source': path_list[:-1], 'destination': path_list[1:]})
df['flag'] = ['Type_1'] + ['-'] * (len(df) - 2) + ['Type_2']
print (df)
source destination flag
0 A A Type_1
1 A B -
2 B C -
3 C C Type_2
设置:
MultiIndex