# Creating new columns with conditions

**URL:** <https://discuss.python.org/t/creating-new-columns-with-conditions/15645>\
**Category:** Python Help\
**Created:** [May 10, 2022, 10:28am UTC](https://discuss.python.org/t/creating-new-columns-with-conditions/15645 "2022-05-10T10:28:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvin\_90](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@arvin\_90](https://discuss.python.org/u/arvin_90)\
**Post date:** [May 10, 2022, 10:28am UTC](https://discuss.python.org/t/creating-new-columns-with-conditions/15645/1 "2022-05-10T10:28:24Z")

</div>

Hey i have three columns (history, amount1 amount2)

History debit credit  
Abc 1000 0  
Xyz 0 1000  
Az 200 0  
Qw 0 200

I want to make new table in which i want

History Debit history credit  
Abc 1000 Xyz 1000  
Az 200 Qw 200

Its not always 0…it could be any number…  
How to solve this?

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 10, 2022, 12:05pm UTC](https://discuss.python.org/t/creating-new-columns-with-conditions/15645/2 "2022-05-10T12:05:42Z")

</div>

If you post the code that you have (just a working model, not like 100s of lines) and I’m sure that someone will be able to help you.

---

<div class="post-metadata">

**Author:** ![arvin\_90](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@arvin\_90](https://discuss.python.org/u/arvin_90)\
**Post date:** [May 10, 2022, 12:25pm UTC](https://discuss.python.org/t/creating-new-columns-with-conditions/15645/3 "2022-05-10T12:25:56Z")

</div>

I completely understand your point but im not able to build the logic…please help

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 10, 2022, 2:30pm UTC](https://discuss.python.org/t/creating-new-columns-with-conditions/15645/4 "2022-05-10T14:30:32Z")

</div>

There’s more than one way to do this, but I’m assuming that you’re a beginner and as such you’re learning the basics.

When planning a program, thing about what data you have, what you plan on doing with said data and what data types you have in order to accomplish the task.

This site covers the basics and much more, but there are others:

> **[Python Tutorial](https://www.w3schools.com/python/default.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

This is a start for you, just to get you going with one data type and how to access the data contained therein.

Apply what you know and learn what you don’t know in order to have a go at your project. Post back with your best effort and we’ll take it from there.

```python
listName = ['Abc', 'Xyz', 'Az', 'Qw']

listDebit = [1000, 0, 200, 0]

listCredit = [0 ,1000, 0, 100]

Abc = str(listName[0]) + ':' + str(listDebit[0]) + ':' + str(listCredit[0])

print(Abc)

```

---

<div class="post-metadata">

**Author:** ![arvin\_90](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@arvin\_90](https://discuss.python.org/u/arvin_90)\
**Post date:** [May 10, 2022, 4:51pm UTC](https://discuss.python.org/t/creating-new-columns-with-conditions/15645/5 "2022-05-10T16:51:56Z")

</div>

Yess im a beginner and im learning and thank you for your help…but in the above query i just made a toy dataset but in real there are many different random values(90000 rows) in all columns so how can i do this

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 10, 2022, 5:01pm UTC](https://discuss.python.org/t/creating-new-columns-with-conditions/15645/6 "2022-05-10T17:01:11Z")

</div>

You’re welcome.

> [@arvin\_90](#):
>
> … there are many different random values(90000 rows) in all columns so how can i do this

By learning the basics first; there are no short cuts if you want to learn how to code.

---

<div class="post-metadata">

**Author:** ![arvin\_90](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@arvin\_90](https://discuss.python.org/u/arvin_90)\
**Post date:** [May 10, 2022, 5:15pm UTC](https://discuss.python.org/t/creating-new-columns-with-conditions/15645/7 "2022-05-10T17:15:04Z")

</div>

Yes yes you are right but this query stucked in my mind and im figuring out how to do

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [May 10, 2022, 5:21pm UTC](https://discuss.python.org/t/creating-new-columns-with-conditions/15645/8 "2022-05-10T17:21:50Z")

</div>

One of the cool things about code is called ‘looping’. This basically means that once you get code to do one thing correctly, you can loop it to do the same thing as many times as you need it to.
