如何将大数组作为列添加到现有csv文件中?

时间:2016-11-06 21:55:55

标签: python csv pandas

我是python和pandas的新手,我将.csv文件的长经纬度列表转换为相应的zipcodes并将结果作为一个大数组获取,现在我想将此zipcodes添加到我的.csv文件中新列名为'zipcode',我该怎么做?提前谢谢!

这是我的csv文件数据

    metrics_time_utc    driver_id   trip_start_time         trip_end_time   trip_start_latitude     trip_start_longitude    trip_end_latitude   trip_end_longitude  trip_distance_travelled     trip_duration   total_fare
0   2016-09-15T00:00:13     436855  2016-09-14T23:52:58Z    2016-09-14T23:59:15Z    37.793320   -122.393291     37.786291   -122.403717     0.817672    0:06:16     10.35
1   2016-09-15T00:00:38     249303  2016-09-14T23:52:35Z    2016-09-15T00:00:04Z    37.798018   -122.406734     37.792295   -122.410805     0.716201    0:07:28     9.90
2   2016-09-15T00:00:53     208378  2016-09-14T23:52:22Z    2016-09-15T00:00:45Z    37.808375   -122.410001     37.795123   -122.394547     1.352469    0:08:23     9.55
3   2016-09-15T00:02:23     436129  2016-09-14T23:41:18Z    2016-09-15T00:01:09Z    37.761789   -122.438659     37.761277   -122.489906     4.575307    0:19:51     23.00
4   2016-09-15T00:02:28     420782  2016-09-14T23:34:40Z    2016-09-15T00:01:55Z    37.792693   -122.401195     37.617482   -122.387628     13.776507   0:27:15     50.75
5   2016-09-15T00:02:28     291046  2016-09-14T23:50:42Z    2016-09-15T00:01:03Z    37.617702   -122.386555     37.614161   -122.405310     4.435810    0:10:20     20.15
6   2016-09-15T00:02:33     341866  2016-09-14T23:47:26Z    2016-09-15T00:02:21Z    37.807505   -122.407165     37.792324   -122.443294     2.894004    0:14:55     14.50
7   2016-09-15T00:03:18     80540   2016-09-14T23:53:27Z    2016-09-15T00:02:51Z    37.783259   -122.440554     37.775283   -122.429431     1.331931    0:09:24     9.55

这是我要在csv文件中插入的输出

[[     0.      0.]
[ 94105.  94103.]
[ 94133.  94108.]
[ 94133.  94133.]
[ 94114.  94122.]
[ 94104.  94128.]
[ 94128.  94066.]
......
......
[ 94128.  94066.]
[ 94118.  94116.]
[ 94109.  94102.]]

1 个答案:

答案 0 :(得分:0)

最好在问题中包含您的代码,否则答案会变成“在黑暗中拍摄”。通常,您的问题可以这样解决:

import pandas as pd
data = pd.read_csv("my.csv")
zipcodes = getzipcodes(data) 
# assuming zipcodes here is 'long array' which length is equal
# to the number of rows in data

data['zipcode'] = zipcodes
data.to_csv("my.csv")