Is there a way to calculate only a partial value of the data frame?

Asked 2 years ago, Updated 2 years ago, 52 views

import pandas as pd

df = pd.DataFrame({"A":[1,4,7,10], "B":[2,5,8,11], "C":[3,6,9,12], "d":[1,5,3,6]})
print(df)

Hi, how are you?

I'm a Python beginner who is receiving a lot of help from hash codes.

As I process the data, I sometimes need to add or subtract some of the values of the DataFrame Series.

Is there any way to deal with this?

More specifically, I'd like to add only 5 and 8 in the "B" column.

I searched on the Internet and found that all rows or columns were combined or calculated.

I can't see is how to handle individual data

and

.

I'd appreciate it if you could answer me.

And I'm always grateful to everyone who answers.

dataframe series

2022-09-20 18:00

1 Answers

I don't know much about Pandas, but... If you look at the DataFrame, there is a list in the dict, so I think you can do it as follows.

"""
The result of print(df) is
{"A":[1,4,7,10], "B":[2,5,8,11], "C":[3,6,9,12], "d":[1,5,3,6]}
"""
print(df['B'][1] + df['B'][2])
# 13 Output


2022-09-20 18:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.