python - 使用python合并多个CSV文件

标签 python csv pandas

我想按如下方式合并三个 CSV 文件:

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

第二个 csv 文件:

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

第三个 csv 文件:

rec = pd.DataFrame()
rec["code"] = ['Tokyo','London','Paris','New_York','Shanghai','LA','Sydney','Berlin','Phoenix','Delhi']
rec["count_A"] = ['1.2','7.8','4','8','7.8','3','8','5','2','10']
rec["count_B] = ['12','78','4','8','78','36,'88,'51,'25,'10']

我试过了。但是我得到了内存错误:

for x in ["board", "off"]:
    df["station"] = df["train_" + x + "_station"]
    df["code"] = df["train_" + x + "_station"]
    df = pd.concat([df, ref,rec], axis=1, join_axes=[df.index])
    df[x + "_point_A"] = df["point_A"]
    df[x + "_point_B"] = df["point_B"]
    df[x + "_count_A"] = df["count_A"]
    df[x + "_count_B"] = df["count_B"]
    df = df.drop(["station", "point_A","point_B","code","count_A","count_B"], axis=1)

我得到内存错误。

最佳答案

看来你需要在循环中使用 df1df2 变量:

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

for x in ["board", "off"]:
    df["code"] = df["train_" + x + "_station"]
    df2 = pd.concat([df, por], axis=1, join_axes=[df.index])
    df[x + "_freq"] = df2["freq"]
    df[x + "_count"] = df2["count"]
    df = df.drop(["code"], axis=1)

print (df)
  train_board_station train_off_station board_latitude board_longitude  \
0               Tokyo           Phoenix         -34.54           34.89   
1                  LA            London         56.789          -78.55   
2               Paris            Sydney         -78,98           78.89   
3            New_York            Berlin          45.62           34.12   
4               Delhi          Shanghai         111.67           56.56   

  off_latitude off_longitude board_freq board_count off_freq off_count  
0       -34.54         34.89        1.2          12      1.2        12  
1       56.789        -78.55        7.8          78      7.8        78  
2       -78,98         78.89          4           4        4         4  
3        45.62         34.12          8           8        8         8  
4       111.67         56.56        7.8          78      7.8        78  

关于python - 使用python合并多个CSV文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43755419/

相关文章:

python - PySerial write() 即时超时

Python Azure 队列,出现错误

python - Matplotlib 在所有子图上显示 x-ticks 和唯一的 y 标签

python - 取 pandas 数据框中两个值的平均值

python - 使用 pandas 读取 Excel 文件时遇到问题

python - 将 Django 与 mod_wsgi 结合使用

python - Statsmodel Z 测试未按预期工作(statsmodels.stats.weightstats.CompareMeans.ztest_ind)

Powershell 不正确的日期排序

python - 在 Python 中覆盖/更改 CSV 上的字段

MySQL CSV 导入 : datetime value