I have multiple (say more than 20/30) CSV files named as 'ABCD.csv','EFGH.csv','IJKL.csv','MNOP.csv' etc in my folder path D:\sevenday . I want to add a column named name in each CSV file and add their respective names inside the CSV file itself. Let us say, ABCD CSV file will have a column named name and it will have ABCD as a name in that column. How could I extract that string information from the file's name in python? Is there a way? There are ways if files are few. But I don't know how to extract many files like that at once as writing each time one by one is tedious.
Asked
Active
Viewed 426 times
0
-
Sure, just write to each of the CSV files and then read the data back. – NewCoder18 Jun 28 '21 at 05:06
-
Thanks. I have tried it. But the problem is there are many CSV files in that path. so, declaring one by one will be tedious. So, I wanted to know how could I declare them to extract them at once and make a function so that I won't have to do it again and again. I've edited the question. thank you. – Jewel_R Jun 28 '21 at 05:16
-
@buran - Hmmm, I think this is incorrect dupe, only part dupe, can you find better one? – jezrael Jun 28 '21 at 05:25
3 Answers
1
Use glob
for get all files from your folder to list, add new columns and write back:
import glob, os
files = glob.glob('D:/sevenday/*.csv')
for fp in files:
name = os.path.basename(fp)
df = pd.read_csv(fp)
df['name'] = name
df.to_csv(fp, index=False)

jezrael
- 822,522
- 95
- 1,334
- 1,252
0
csv_file = "ABCD.csv"
df = pd.read_csv(csv_file)
df["name"] = csv_file.replace(".csv","")

Bharat Agrawal
- 125
- 3
- 11
-
1Thanks. I have tried it. But the problem is there are many csv files in that path. so, declaring one by one will be tedious. So, I wanted to know how could I declare them (the 1st line in the code) to extract them at once and make a function like your code so that I wont have to do it again and again. I've edited the question. thank you. – Jewel_R Jun 28 '21 at 05:15
-
-
How can I use for loop here in this line when `csv_file = "ABCD.csv"` I have to write every name manually. Because if there are more than 30 files don't I have to write each CSV file name one by one? I was confused here. – Jewel_R Jun 28 '21 at 05:21
-
@JewelRanaPalit please check my new code comment. if you like it please give +1 – Bharat Agrawal Jun 28 '21 at 05:25
-
I did. BUt to be counted It says I need at least 15 repu. I am new here yet.Thank you a lot – Jewel_R Jun 28 '21 at 05:27
-
I did not get you in last comment. did you get the result or not? – Bharat Agrawal Jun 28 '21 at 05:30
-
This shows an error like `df.to_csv(csv_file. index=False) ^ SyntaxError: keyword can't be an expression` – Jewel_R Jun 28 '21 at 13:19
-
0
import glob
folder_path = 'D:\\sevenday'
file_type = '\*.csv'
files = glob.glob(folder_path + file_type)
for csv_file in files:
df = pd.read_csv(csv_file)
df["name"] = csv_file.split("\\")[-1].replace(".csv","")
df.to_csv(csv_file, index=False)

Bharat Agrawal
- 125
- 3
- 11