You can first select by iloc and then sum:
df['Fruit Total']= df.iloc[:, -4:-1].sum(axis=1)
print (df)
Apples Bananas Grapes Kiwis Fruit Total
0 2.0 3.0 NaN 1.0 5.0
1 1.0 3.0 7.0 NaN 11.0
2 NaN NaN 2.0 3.0 2.0
For sum all columns use:
df['Fruit Total']= df.sum(axis=1)
Answer from jezrael on Stack OverflowYou can first select by iloc and then sum:
df['Fruit Total']= df.iloc[:, -4:-1].sum(axis=1)
print (df)
Apples Bananas Grapes Kiwis Fruit Total
0 2.0 3.0 NaN 1.0 5.0
1 1.0 3.0 7.0 NaN 11.0
2 NaN NaN 2.0 3.0 2.0
For sum all columns use:
df['Fruit Total']= df.sum(axis=1)
This may be helpful for beginners, so for the sake of completeness, if you know the column names (e.g. they are in a list), you can use:
column_names = ['Apples', 'Bananas', 'Grapes', 'Kiwis']
df['Fruit Total']= df[column_names].sum(axis=1)
This gives you flexibility about which columns you use as you simply have to manipulate the list column_names and you can do things like pick only columns with the letter 'a' in their name. Another benefit of this is that it's easier for humans to understand what they are doing through column names. Combine this with list(df.columns) to get the column names in a list format. Thus, if you want to drop the last column, all you have to do is:
column_names = list(df.columns)
df['Fruit Total']= df[column_names[:-1]].sum(axis=1)
python - Pandas - Sum of multiple specific columns - Data Science Stack Exchange
python - Pandas : Sum multiple columns and get results in multiple columns - Stack Overflow
Pandas: sum up multiple columns into one column without last column
pandas - sum of values in column for each unique combination of values from two other columns
When you pass a dictionary or callable to groupby it gets applied to an axis. I specified axis one which is columns.
d = dict(A='AB', B='AB', C='CD', D='CD')
df.groupby(d, axis=1).sum()
Use concat with sum:
df = df.set_index('idx')
df = pd.concat([df[['A', 'B']].sum(1), df[['C', 'D']].sum(1)], axis=1, keys=['AB','CD'])
print( df)
AB CD
idx
J 3 4
K 9 8
L 15 12
M 3 7
N 9 11
O 15 15
Hello,
I have a DataFrame with multiple columns, where two of them are strings, one is date and one is int:
name 1 name 2 value date [other columns...] A X 1 01.03.2022 A Y 1 11.03.2022 B Y 2 22.02.2022 A Z 2 05.02.2022 A Z 3 10.02.2022 B X 2 12.03.2022 C Y 2 11.03.2022 B Z 3 21.01.2022 B Y 1 10.01.2022 A X 5 15.01.2022 C X 1 08.02.2022 C X 1 10.01.2022 [...]
And I need to get something like this, for each specified period (start and end parameters dynamically provided):
A B C sum X 6 2 2 10 Y 1 3 2 6 Z 5 3 0 8
Rows should be filtered by start=<date<=end, and for each value in combination of "name 1" and "name 2" I need to get sum of "value" and also total sum for each "name 2". If combination does not exists in DataFrame sum should be filled with 0.
There will be approx. 20 unique values in "name 1", 50 unique values in "name 2", "value" will be in 1-5 range, and there should be more or less 900 rows.
I am new to pandas. Could you please point me in right direction? What will be correct way to approach the problem? What functions/methods should I use?