0

I have .csv file with colums A and B, and rows with values.

A, B
232.65, -57.48
22.69, -5.46
23.67, -7.71

I want to loop these values with function.

So lets create simple function with 2 positional parameters:

def add_numbers(n1, n2):
    answer = n1+n2
    print(answer)
#lets read the file:
import pandas as pd
df= pd.read_csv(r'myfile.csv')

print(df[A]) #just checking the column A for this example, but column B is also required for n2.
0   232.65
1   22.69
3   23.67


  1. I could also transfer those colums to array but I still could not loop it. How would I loop these with the function that requires two arguments?

arr = np.loadtxt(r'myfile.csv', delimiter=',')
arr

array([[232.65, -57.48],
       [22.69, -5.46],
       [23.67, -7.71],

I have been trying to loop with various ways and iter and enumerate and apply, but I keep doing something slightly wrong.

Cheer, Joonatan

3
  • 1
    Check out stackoverflow.com/a/16476974/2794417 on how to loop throughj dataframe. Check out: stackoverflow.com/a/37428259/2794417 on how to apply a function to all rows of dataframe. Commented Feb 13, 2023 at 3:52
  • 1
    Does this answer your question? How to iterate over rows in a DataFrame in Pandas Commented Feb 13, 2023 at 3:54
  • I did read those before. My issue was that I basically know how to iterate, but not with a function. I can do it with list when only 1 positional argument is required. But not with df when there are two positional arguments required. Well I will have to continue then reading tomorrow. It is just that those samples were "how to edit" dataframe with iterating, for example adding colums. That I can do. Or how to loop list with SINGLE positional argmunt. I have now searched 14 hours answer for this, so I need to continue tomorrow. Commented Feb 13, 2023 at 4:09

3 Answers 3

1

You can loop and pass the values in each row to add_numbers. Then use iterrows to get the index and row values.

def add_numbers(n1, n2):
answer = n1 + n2
print(answer)
    
import pandas as pd
df = pd.read_csv(r'myfile.csv')
    
for index, row in df.iterrows():
    add_numbers(row['A'], row['B'])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! This was just what I was looking for! Thank you everyone for their time and answers. This helps me a lot.
1

I hope, it works for your solution. Whenever you have to apply a custom function and want to loop through values to perform an operation use apply function.

Code:

import pandas as pd
df = pd.read_csv('./loop_through_2_columns.csv')
def add_numbers(n1, n2):
    answer = n1+n2
    return answer
df['result'] = df.apply(lambda x: add_numbers(x.A, x.B), axis=1) 
df.head()

Output:

      A       B     result
0   232.65  -57.48  175.17
1   22.69   -5.46   17.23
2   23.67   -7.71   15.96

Comments

1

try:

import pandas as pd

df = pd.DataFrame({
    'A': [232.65,22.69,23.67],
    'B':[-57.48,-5.46,-7.71]
})


def add_numbers(n1, n2):
    answer = n1+n2
    print(answer)

for i in range(len(df)):
    n1 = df.loc[i,'A']
    n2 = df.loc[i,'B']
    add_numbers(n1,n2)

output:

175.17000000000002
17.23
15.96

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.