找到所有具有匹配值的行,将所有匹配值输出到sam

2024-09-28 21:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我从不使用python,所以不知道如何解决这个问题。我有一个excel/csv文件,格式如下。我需要找到所有具有匹配infid值的行,并将这些行输出到新文件中的同一行。你知道吗

你知道吗我的文件.csv地址:

ROUTE_NAME  CURR_VOL    IN_FID      NEAR_RANK
test11      test11      1           test11
test12      test12      1           test12
test2       test2       2           test2
test3       test3       3           test3
test31      test        3           test31

期望输出:

IN_FID   ROUTE_NAME1    NEAR_RANK1     ROUTE_NAME2     NEAR_RANK2
1        test11         test11         test12          test12
2        test2          test2          null            null
3        test3          test3          test31          test31

我一开始只是尝试用python操作csv,但我想知道是否有像pandas这样的库更适合使用?你知道吗

#!/usr/bin/python
import csv
profile_to_search = input()

with open('myfile.csv', 'rt') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        if profile_to_search == row[2]:
            print(row)

我走了这么远,然后意识到我不知道自己在做什么。你知道吗


Tags: 文件csvinprofileroutenullreaderrow
2条回答

只是一个cumcount那么应该是简单的pivot问题

df['Key']=df.groupby('IN_FID').cumcount()+1
s=df.pivot_table(index='IN_FID',columns='Key',values=['ROUTE_NAME','NEAR_RANK'],aggfunc='first')
s=s.sort_index(level=1,axis=1)
s.columns=s.columns.map('{0[0]}_{0[1]}'.format)
s
       NEAR_RANK_1 ROUTE_NAME_1 NEAR_RANK_2 ROUTE_NAME_2
IN_FID                                                  
1           test11       test11      test12       test12
2            test2        test2        None         None
3            test3        test3      test31       test31

如果我了解您想要的是什么…假设您想要的输出缺少CURR_VOL列:

# read your csv file
df = pd.read_csv(r'path\to\your\file.csv')

df['idx'] = df.groupby('IN_FID').cumcount()

# set index and unstack
new = df.set_index(['idx', 'IN_FID']).unstack(level=[0])

# list comprehension to create one column
new.columns = [f'{val}_{name}' for val, name in new.columns]

# output a new csv file
new.to_csv(r'some\path\to\new_file.csv')

       ROUTE_NAME_0 ROUTE_NAME_1 CURR_VOL_0 CURR_VOL_1 NEAR_RANK_0 NEAR_RANK_1
IN_FID                                                                        
1            test11       test12     test11     test12      test11      test12
2             test2          NaN      test2        NaN       test2         NaN
3             test3       test31      test3       test       test3      test31

更有效的方法是使用map

# group with astype(str)
df['idx'] = df.groupby('IN_FID').cumcount().astype(str)

# set index and unstack
new = df.set_index(['idx', 'IN_FID']).unstack(level=[0])

# more efficient using map
new.columns = new.columns.map('_'.join)

相关问题 更多 >