Python如何在不同文件的CSV文件中添加列?

时间:2018-05-16 07:17:33

标签: python pandas csv

我有4个CSV文件。 我能够合并所有4个,但它有问题。

这是一个名为services.csv

的文件
ServiceID   Service
1   General Practitioner
2   Pathology
3   Radiology
4   Psychiatry
5   Chiropratic

和另一个文件(“我把它命名为test.csv”)

ClinicServiceID ClinicID    ServiceID   Name    Suburb  State   Postcode    Email   Lat Lon
1   1   1   Hurstville Clinic   Hurstville  NSW 1493    hurstville@myclinic.com.au  -33.975869  151.088939
2   1   2   Hurstville Clinic   Hurstville  NSW 1493    hurstville@myclinic.com.au  -33.975869  151.088939
3   2   1   Sydney Centre Clinic    Sydney  NSW 2000    sydney@myclinic.com.au  -33.867139  151.207114
4   2   2   Sydney Centre Clinic    Sydney  NSW 2000    sydney@myclinic.com.au  -33.867139  151.207114
5   2   3   Sydney Centre Clinic    Sydney  NSW 2000    sydney@myclinic.com.au  -33.867139  151.207114

现在我必须根据测试文件中的ServiceID从Service.csv文件中添加服务列。

我能够合并所有文件,但我不知道如何执行上述操作。

Things that I Have to achieve:
1) Add Service column into the test.csv file
2) make the entries in the Service Column using the Service.csv file data.

任何人都可以帮助我。我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

我认为DataFrame ServiceID的{​​{1}}需要read_csv,而df1 = pd.read_csv('services.csv') df2 = pd.read_csv('test.csv') #get position of ServiceID column pos = df2.columns.get_loc('ServiceID') + 1 df2.insert(pos, 'Service', df2['ServiceID'].map(df1.set_index('ServiceID')['Service'])) print (df2) ClinicServiceID ClinicID ServiceID Service \ 0 1 1 1 General Practitioner 1 2 1 2 Pathology 2 3 2 1 General Practitioner 3 4 2 2 Pathology 4 5 2 3 Radiology Name Suburb State Postcode \ 0 Hurstville Clinic Hurstville NSW 1493 1 Hurstville Clinic Hurstville NSW 1493 2 Sydney Centre Clinic Sydney NSW 2000 3 Sydney Centre Clinic Sydney NSW 2000 4 Sydney Centre Clinic Sydney NSW 2000 Email Lat Lon 0 hurstville@myclinic.com.au -33.975869 151.088939 1 hurstville@myclinic.com.au -33.975869 151.088939 2 sydney@myclinic.com.au -33.867139 151.207114 3 sydney@myclinic.com.au -33.867139 151.207114 4 sydney@myclinic.com.au -33.867139 151.207114 后的新列需要map

select * from table where (x = 1 OR y = 1) AND starting_at < '2018-05-01'