使用python组合两个文件?

时间:2017-05-01 13:04:48

标签: python csv pandas for-loop valueerror

我使用pycharm版本2.7.9和pandas版本 - 0.19.2。 我有一个名为''data'的csv文件,其中包含有纬度和经度的电台名称数据。

df = pd.read_csv(data.csv, sep="^", dtype=object)
df["train_board_station"] = ['Tokyo','LA','Paris','New_York','Delhi']
df["train_off_station"] = ['Phoenix','London','Sydney','Berlin','Shanghai']

我采用另一个csv文件,如果'train_board_station'和'train_off_station'与'station'相同,则会向其添加纬度和经度数据。

ref = pd.read_csv(ref.csv, sep="^", header=0, dtype=str)
df["station"] = ['Tokyo','London','Paris','New_York','Shanghai,''LA','Sydney','Berlin','Phoenix','Delhi']
df["latitude"] = ['-34.54','56.789','-78,98','45.62','111.67','23.78','-98.40','-76.89','23.98','23.89']
df["longitude"] = ['34.89','-78.55','78.89','34.12','56.56','23.23','-78.65','34.76','23.67','21.645']

如果'train_board_station','train_off_station'与data.csv中的'station'匹配,我想从'ref.csv'文件合并'latitude'和'longitude'

for x in ["board", "off"]:
    df["station"] = df["train_" + x + "_station"]
    df = pd.concat([df, ref], axis=1, join_axes=[df.index])
    df[x + "_latitude"] = df["latitude"]
    df[x + "_longitude"] = df["longitude"]

当我尝试运行代码时。我得到错误 -

  KeyError: 'train_board_station'

2 个答案:

答案 0 :(得分:0)

以下是使用.join函数的示例。首先是最小化的设置。

import pandas as pd

df = pd.DataFrame({
    "station": ["Tokyo", "Amsterdam"],
    "size": [100, 200]
})

ref = pd.DataFrame({
    "train_station": ["Amsterdam", "Tokyo"],
    "lon": [23, 12],
    "lat": [34, 12]
})

加入的关键是在两个数据帧上设置索引。

df.set_index("station").join(ref.set_index("train_station"))

<强>退出

enter image description here

答案 1 :(得分:0)

df = pd.DataFrame()
df["train_board_station"] = ['Tokyo','LA','Paris','New_York','Delhi']
df["train_off_station"] = ['Phoenix','London','Sydney','Berlin','Shanghai']

#need ref DataFrame
ref = pd.DataFrame()
ref["station"] = ['Tokyo','London','Paris','New_York','Shanghai','LA','Sydney','Berlin','Phoenix','Delhi']
ref["latitude"] = ['-34.54','56.789','-78,98','45.62','111.67','23.78','-98.40','-76.89','23.98','23.89']
ref["longitude"] = ['34.89','-78.55','78.89','34.12','56.56','23.23','-78.65','34.76','23.67','21.645']

我相信你可以在循环中使用join

for x in ["board", 'off']:
    val = "train_" + x + "_station"
    df1 = df[[val]].join(ref.set_index('station'), on=val)
    df[x + "_latitude"] = df1["latitude"]
    df[x + "_longitude"] = df1["longitude"]
print (df)
  train_board_station train_off_station board_latitude board_longitude  \
0               Tokyo           Phoenix         -34.54           34.89   
1                  LA            London          23.78           23.23   
2               Paris            Sydney         -78,98           78.89   
3            New_York            Berlin          45.62           34.12   
4               Delhi          Shanghai          23.89          21.645   

  off_latitude off_longitude  
0        23.98         23.67  
1       56.789        -78.55  
2       -98.40        -78.65  
3       -76.89         34.76  
4       111.67         56.56