Python Forum
Add a row to a dataframe or append whole dataframe.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add a row to a dataframe or append whole dataframe.
#1
Hello to all,
Happy new year and good health!!


I am sure this is super simple, but I can't find the right way to do it.
I have a folder with some csv files.
All csv files have the same structure, same number of columns, similar data but the number of rows are different.
I want to regroup all that data in one dataframe to make a single CSV.

I have the following code.

import pandas as pd
import glob
path = r"path\to\my\folder\*.csv"
df2 = pd.DataFrame(columns=['Name','Reading', 'Office','Phone','address' ])

csvfiles = []
a=0
for file in glob.glob(path):
    csvfiles.append(file)
   # print(file)

for csvnub in csvfiles:
   df=pd.read_csv(csvnub)
   count_row = df.shape[0]

   print(count_row)
   df2.iloc[a] = df.iloc[count_row]
   a=a+1

print(df2)
df2.to_csv("InfosTotal.csv", index=True, encoding="utf_8_sig")
I tried append()...

for csvnub in csvfiles:
   df=pd.read_csv(csvnub)
   count_row = df.shape[0]
   print(count_row)
   for row in count_row:
       df2.append(row) 
or

df2.append(df, ignore_index = False) 
I tried concat() also...

Help will be extremely appreciated.
Reply
#2
OK,
I found the solution myself, I don't know if this is clean, proper but it works.
Never touch a working script...
hahaha
ok,
I am sure this will help others.

import pandas as pd
import glob
path = r"path\to\my\folder\*.csv"
df2 = pd.DataFrame(columns=['Name','Reading', 'Office','Phone','address' ])

csvfiles = []
for file in glob.glob(path):
    csvfiles.append(file)
   # print(file)

for csvnub in csvfiles:
   df=pd.read_csv(csvnub)
   count_row = df.shape[0]
   print(count_row)
   df2=pd.concat([df, df2])
   

df2.sort_values(by=['Name'])
df2.drop_duplicates(keep='first', inplace=True)
df2.to_csv("InfosTotal.csv", index=True, encoding="utf_8_sig")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Converting Pandas DataFrame to a table of hourly blocks Abedin 1 578 Apr-24-2025, 01:05 PM
Last Post: snippsat
  renaming a column without a name in a dataframe Carbonpony 2 1,070 Jan-23-2025, 08:20 AM
Last Post: Carbonpony
  dataframe merge gunther 2 634 Jan-22-2025, 05:23 PM
Last Post: Pedroski55
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 2,401 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
  Most efficient way to roll through a pandas dataframe? sawtooth500 2 1,179 Aug-28-2024, 10:08 AM
Last Post: Alice12
  Confused by the different ways of extracting data in DataFrame leea2024 1 713 Aug-17-2024, 01:34 PM
Last Post: deanhystad
  docx file to pandas dataframe/excel iitip92 1 2,769 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries sawtooth500 14 8,376 Apr-24-2024, 01:42 AM
Last Post: sawtooth500
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 2,807 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Dataframe copy warning sawtooth500 4 2,315 Mar-25-2024, 11:38 PM
Last Post: sawtooth500

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020