# Add a row to a dataframe

**URL:** https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170
**Category:** General Usage
**Tags:** question, dataframes
**Created:** [June 1, 2021, 3:44am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170 "2021-06-01T03:44:12Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [June 1, 2021, 3:44am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170/1 "2021-06-01T03:44:12Z")

</div>

```julia
spreadsheet = DataFrame(
date=["Jan 1, 2019"], #start of the new year
	
	
	#List of Accounts # Start of 2019
	
	

#Assets
	
chequing=[0], #replace with balance as of Jan 1, 2019

accounts_reciveable=[0], #replace with balance as of Jan 1, 2019
	
#Debts
#Debts--Credit when owed payment, Debit when recive payment

accounts_payable= [0],# be sure to add account as of Jan1, 2019

#Expenses -- Debit
	
personal=[0],

#income credit
	
wages=[0],

rent=[0],

cattle_sales=[0],
	

#Debts--Credit when owed payment, Debit when recive payment

#memo

memo=["puchase item and place"]
	);

defaultRow = (DataFrame(
date=[""],
chequing=[0],
accounts_recivable=[0],
accounts_payable=[0],
personal=[0],
wages=[0],
rent=[0],
catle_sales=[0],
memo=[""]))

macro transaction(
		Journal_Entry,
		newdate,
		newdebit,
		debit_amount,
		newcredit, 
		credit_amount,
		new_memo)
	
	ex=quote
 	   if $credit_amount != $debit_amount
			"Error--Debit and Credit must match"
       else
			
				push!(spreadsheet,(defaultRow...,
					$newcredit=$credit_amount,		
					$newdebit= $debit_amount,
					Date=$newdate,
					memo=$new_memo
						)) 

			#figure out how to delete this row, and get around repeat variables
			# I might have to save each transaction to the spreadsheet, at this stage
		end
	end
	esc(ex)
	
end

@transaction(JE4,"Jan 1, 2019", wages, 1000, chequing, 1000,"this is a test")

```

Why can’t I get this to work, should the default row be a data frame? Is there a difference in the number of variables? I counted them 20 times. Each has 9 variables, maybe there is a spelling error?

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [June 1, 2021, 4:17am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170/2 "2021-06-01T04:17:49Z")

</div>

According to `@macroexpand`, that expands to

```julia
julia> push!(spreadsheet, (defaultRow..., chequing = 1000, wages = 1000, Date = "Jan 1, 2019", memo = "this is a test"))
ERROR: AbstractDataFrame is not iterable. Use eachrow(df) to get a row iterator or eachcol(df) to get a column iterator

```

The error is happening in the `defaultRow...,` segment.

This will be easier to see if you don’t use a macro. A function seems like a better fit.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [June 1, 2021, 4:25am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170/3 "2021-06-01T04:25:20Z")

</div>

This whole thing was a mess. I got it all to work,

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [June 1, 2021, 4:54am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170/4 "2021-06-01T04:54:29Z")

</div>

Might look at

> [@How to warn new users away from metaprogramming](https://discourse.julialang.org/t/how-to-warn-new-users-away-from-metaprogramming/35022):
>
> [Metaprogramming](https://docs.julialang.org/en/v1/manual/metaprogramming/) is a great feature of Julia, allowing advanced users to introduce syntactic extensions with macros, and work around code optimization problems using generated functions. However, it is somewhat of an advanced technique with particular pitfalls, and in practice it is needed much less frequently than some new users of Julia realize, possibly because the basic building blocks of Julia are so powerful. From some discussions on this forum, it appears metaprogramming is considered as …

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [June 1, 2021, 5:24am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170/5 "2021-06-01T05:24:06Z")

</div>

Is metaprograming different than macros?

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [June 1, 2021, 5:40am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170/6 "2021-06-01T05:40:32Z")

</div>

I’d say macros are a prominent kind of metaprogramming (there are [other kinds](https://en.wikipedia.org/wiki/Metaprogramming#Uses_in_programming_languages)). [Generated functions](https://docs.julialang.org/en/v1/manual/metaprogramming/#Generated-functions) are another (though often created using macros).

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [June 1, 2021, 5:44am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170/7 "2021-06-01T05:44:19Z")

</div>

I can see where that is useful. I think I can do almost everything with a macro, and I want to get that figured out first. The problem I’m having now, is entering the name of an array or a DataFrame column as a string.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [June 1, 2021, 5:47am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170/8 "2021-06-01T05:47:28Z")

</div>

> [@brett\_knoss](#):
>
> I think I can do almost everything with a macro

To be clear, my point is that macros are almost always unnecessary and create confusion. I think this is an example of that: unless I’m misunderstanding your goal, there is no need to use a macro here, and you shouldn’t use one. A function (defined with `function`) would be fine.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [June 1, 2021, 10:38am UTC](https://discourse.julialang.org/t/add-a-row-to-a-dataframe/62170/9 "2021-06-01T10:38:16Z")

</div>

Here (in this thread, out of 3) we get the `defaultRow` shown, which I was missing in the other thread and which I asked for. With this information, I can try to help perhaps a step further.

I understand it like, `defaultRow` holds the default information, which should be pushed into the DataFrame, if only parts of the new row is given. With that, `defaultRow` should be possible to be pushed, even if no information is given:

```julia
julia> push!(spreadsheet,defaultRow)
ERROR: ArgumentError: `push!` does not allow passing collections of type DataFrame to be pushed into a DataFrame. Only `Tuple`, `AbstractArray`, `AbstractDict`, `DataFrameRow` and `NamedTuple` are allowed.

```

From this the answer is clear: not a DataFrame can be pushed, only a DataFrameRow. So:

```julia
julia> push!(spreadsheet,defaultRow[1,:])
┌ Error: Error adding value to column :accounts_reciveable.
ERROR: ArgumentError: column name :accounts_reciveable not found in the data frame; existing most similar names are: :accounts_recivable

```

Again clear, a typo, fixing that:

```julia
defaultRow = (DataFrame(
date=[""],
chequing=[0],
accounts_reciveable=[0],
accounts_payable=[0],
personal=[0],
wages=[0],
rent=[0],
catle_sales=[0],
memo=[""]))

julia> push!(spreadsheet,defaultRow[1,:])
┌ Error: Error adding value to column :cattle_sales.
ERROR: ArgumentError: column name :cattle_sales not found in the data frame; existing most similar names are: :catle_sales

```

Another one, fixing that too:

```julia
defaultRow = (DataFrame(
date=[""],
chequing=[0],
accounts_reciveable=[0],
accounts_payable=[0],
personal=[0],
wages=[0],
rent=[0],
cattle_sales=[0],
memo=[""]))

julia> push!(spreadsheet,defaultRow[1,:])
2×9 DataFrame
...

```

Does this help?  
Two points:

- To stress the point: macros aren’t the ultimate tool to achieve code reuse, it’s functions which do this.
- It’s currently not clear, if your problem is solved or not.
