Home > Software engineering >  How can I add two for loop list to one?
How can I add two for loop list to one?

Time:05-08

So, I creating two list with a for loop, but I don't know how can I add them together.

    def month_changed(cls):

    df = pd.read_excel("income.xlsx")
    df1 = df.iloc[:13].values

    for x in range(12):
        try:
            df2 = sum(int(i) for i in df1[x, 1:])
            mylist = [df2]
            print(mylist)

        except ValueError:
            print("cannot convert float NaN to integer")

output: [21288] [21445]

desired output: [21288,21445]

Any idea?

CodePudding user response:

You can do that a lot easier than looping through lists. You already have the dataframe df1 after pd.read_csv.


df1['sum'] = df1[df1.columns[1:]].sum(axis=1)
# 'sum' will be new column
# df1[df1.columns[1:]] will skip the first column
# sum(axis=1) will add all values together along the axis (horizontally)
# df1['sum'] is a pd.Series, if you want a list just do:
result_list = df1['sum'].tolist()

print(result_list)
e.g [1,23,35,57]
#since you have 13 rows, this list should have 13 elements

EDIT: just to show you how you would get your code working, only little changings. I created a little test dataframe.

df = pd.DataFrame(
    {'dummy': {0: 0, 1: 1, 2: 2, 3: 3},
     'col1': {0: 1.0, 1: 1.0, 2: 3.2, 3: 3.0},
     'col2': {0: 2.4, 1: 3.0, 2: 4.5, 3: 7.0},
     'col3': {0: 12.3, 1: 11.0, 2: 1.7, 3: 3.3}})

result_sum1, result_sum2, result_sum3 = [], [], []
for x in range(len(df)):
    try:
        # 2 examples how you get your sum
        row_sum1 = sum(int(i) for i in df.iloc[x, 1:])
        row_sum2 = sum(map(int, df.iloc[x, 1:]))
        
        # if you leave it with floats just do:
        row_sum3 = sum(df.iloc[x, 1:])
        
        result_sum1.append(row_sum1)
        result_sum2.append(row_sum2)
        result_sum3.append(row_sum3)
    except ValueError:
        print("cannot convert float NaN to integer")
        
print(f"1: {result_sum1}")
print(f"2: {result_sum2}")
print(f"3: {result_sum3}")

[15, 15, 8, 13]
[15, 15, 8, 13]
[15.700000000000001, 15.0, 9.4, 13.3]

  • Related