calculation error for calculating per panas Python data frame column

Asked 2 years ago, Updated 2 years ago, 58 views

I want to create a column of (downstream value*1 + middlestream value*2 + upstream value*3)/ 3 from this data The error unsupported operation type(s) for /: 'list' and 'int' continues to appear

//result1 = ta1_sample["downstream"*1 + "middlestream"*2 + "upstream"*3]/3] 

Isn't this how you make the code? In the first file, the type is object, so I changed all of those numerical values to int32. But I don't know why I keep getting errors.

And I thought I should calculate it with a column value, so I calculated it after changing the matrix using transpose() from the existing data.

Can we still create columns or rows calculated as above?

pandas dataframe

2022-09-20 10:26

1 Answers

You have to do it like the bottom.

>> df = pd.dataFrame(np.random.randint(100, 1000, size=(5,3)), columns=["downstream", "upstream", "upstream", index=[f"{year}"2006, 2006+year"inward]
>>> df
        downstream and upstream
2006 819 530735
2007 712 319820
2008 932 892 630
In 2009, 53711939
2010 913 400 218
>>> result = (df["downstream"]*1 + df["middlestream"]*2 + df["downstream"]*3)/3
>>> result
2006 1361.333333
2007 1270.000000
In 2008, 1535.33333
2009: 1592.000000
2010 789.000000
dtype: float64

It seems more cumbersome to calculate with a transparent data frame. Calculate it in its original form, and transform it when necessary.


2022-09-20 10:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.