CSV文件为特定格式

时间:2018-12-11 21:16:30

标签: python pandas csv formatting

我有一个像这样的文本文件:

APAC230_WINC230,P1-2,Transline,17002,APACHE,230,17105,WINCHSTR,230,1
WINC345_VAIL345,P1-2,Transline,16109,WINCHSTR,345,16105,VAIL,345,1
WINC345_VAIL345,P1-2,Transline,16109,WINCHSTR,345,16105,VAIL,345,1a

我希望能够将列表转换为如下形式:

APAC230_WINC230,P1-2
Transline,17002,APACHE,230,17105,WINCHSTR,230,1
WINC345_VAIL345,P1-2
Transline,16109,WINCHSTR,345,16105,VAIL,345,1
Transline,16109,WINCHSTR,345,16105,VAIL,345,1a

使用大熊猫read_CSV可以创建与上面类似的列表,但是遇到具有多个元素的实体时会遇到问题。

例如,这是我可以创建的输出:

APAC230_WINC230,P1-2
Transline,17002,APACHE,230,17105,WINCHSTR,230,1
WINC345_VAIL345,P1-2
Transline,16109,WINCHSTR,345,16105,VAIL,345,1
WINC345_VAIL345,P1-2
Transline,16109,WINCHSTR,345,16105,VAIL,345,1a

我正在处理非常大的列表,所以我很难简单地删除重复项,而且实体也具有变体名称。

这里是我的代码:

import pandas as pd 
def cgy(input_file):
    rows=['cgy','cat_con_evt','type','frombusid','frombus','frombuskv',
    'tobusid','tobus','tobuskv','circuitid']
    df = pd.read_csv(input_file,names=rows,dtype=object)
    cgy_file = ""
    cgy_file = input("Enter output file name:")
    with open(cgy_file, 'w') as f:
        for i in range(0,len(df)):
            print(df.loc[i]['cgy']+","+df.loc[i]['cat_con_evt'], file=f)
            print(df.loc[i]['type']+","+
            df.loc[i]['frombusid']+","+df.loc[i]['frombus']+","+df.loc[i]['frombuskv']+","+
            df.loc[i]['tobusid']+","+df.loc[i]['tobus']+","+df.loc[i]['tobuskv']+","+df.loc[i]['circuitid'],file=f)
def main():

    input_file = ""
    input_file = input("Enter input file name: ")
    cgy(input_file)
if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

我建议创建一个具有2列的数据框,其中包含文本文件中每行的前2个和后8个元素。

复制文本文件数据:

APAC230_WINC230,P1-2,Transline,17002,APACHE,230,17105,WINCHSTR,230,1
WINC345_VAIL345,P1-2,Transline,16109,WINCHSTR,345,16105,VAIL,345,1
WINC345_VAIL345,P1-2,Transline,16109,WINCHSTR,345,16105,VAIL,345,1a

并运行以下代码:

# import data
df = pd.read_clipboard(sep=',',header =None, names = ['cgy','cat_con_evt','type','frombusid','frombus','frombuskv',
    'tobusid','tobus','tobuskv','circuitid'])
# convert all columns to string
df = df.applymap(str)
# create new columns 'A' and 'B' as explained
columnsA = ['cgy','cat_con_evt']
columnsB = ['type','frombusid','frombus','frombuskv','tobusid','tobus','tobuskv','circuitid']
df['A'] = df[columnsA].apply(lambda x: ','.join(x.fillna('')), axis=1)
df['A'] = df['A'].str.strip(',')
df['B'] = df[columnsB].apply(lambda x: ','.join(x.fillna('')), axis=1)
df['B'] = df['B'].str.strip(',')
# drop useless columns
df = df.drop(columnsA + columnsB , axis=1).sort_values('A')
# print desired output
for x in df.A.unique().tolist():
    print(x)
    l = df[df['A']==x]['B'].tolist()
    for y in l:
        print(y)