Add column from another csv file

24 views
Skip to first unread message

Dodi Ardiansyah

unread,
Sep 21, 2020, 11:37:54 PM9/21/20
to PyData
Dear, can any one help me?
I have some csv files, lets say now i have three csv files in one folder with consist of three columns each file. My folder:d\learning\ 1.csv 2.csv 3.csv A B C A B C A B C 5 23 56 5 43 23 5 65 08 10 31 77 10 76 66 10 34 72 20 33 98 20 39 28 20 23 64
30 18 26 30 27 39 30 73 92 Then i want to make a new.csv file by adding B columns from another files using looping. desired result : new.csv A B B B 5 23 43 65 10 31 76 34 20 33 39 23 30 18 27 73
19:05

19:05
I have tried but failed

So, this my script below :

import pandas as pd import numpy as np import csv import glob import os path = "C:/Users/SYIFAAZRA/Documents/belajar_wradlib/learning/" os.chdir(path) file = glob.glob("*.csv") one = { 'A' : ['5','10','20','30'] } i = 1 for f in file: i = i+1 col_names = ['B', 'C'] df = pd.read_csv(f, delimiter=',',usecols=[1, 2], names=col_names)
df = pd.DataFrame(one) df['B'] = pd.Series(df)
print(df)

Paul Hobson

unread,
Sep 22, 2020, 12:55:33 AM9/22/20
to pyd...@googlegroups.com
Hey Dodi,

This is one of the top-level topics in the "Getting Started" section of the pandas docs:


--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pydata/79608133-c3bd-4b9f-bf1d-9f5e9473a95eo%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages