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


Messages In This Thread
Add a row to a dataframe or append whole dataframe. - by tsurubaso - Jan-06-2021, 02:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas dictionary dataframe help michaelserra 4 161 Jun-19-2021, 10:26 AM
Last Post: michaelserra
  AttributeError: 'DataFrame' object has no attribute 'Articles' Anldra12 5 252 Jun-17-2021, 04:54 PM
Last Post: Larz60+
  [solved] dataframe and read_csv ju21878436312 4 288 Jun-16-2021, 06:01 AM
Last Post: ju21878436312
  How to rename dataframe columns based on the content in an index? ar_mahdavi 2 439 Jun-07-2021, 06:09 AM
Last Post: ricslato
  Reading data to python: turn into list or dataframe hhchenfx 2 179 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 240 May-04-2021, 10:51 PM
Last Post: rhat398
  How to get value in Dataframe given row & column values? moonlight 1 265 Apr-26-2021, 09:30 PM
Last Post: Larz60+
  dataframe cell conditional format by row SriRajesh 0 362 Apr-24-2021, 11:51 AM
Last Post: SriRajesh
Star Split and organize my Pandas Dataframe brunolelli 4 410 Apr-18-2021, 03:00 AM
Last Post: brunolelli
  How to save json data in a dataframe shantanu97 1 332 Apr-15-2021, 02:44 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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