0

I'm new to pandas and the dataframe-concept. Because of the format of my data (excel-sheets, first row is the name of my data, the second row is the unit) it's a little tricky to handle it in a data frame. The task is to calculate new data from existing columns, e.g. df.['c'] = df['a']**2 + df.['b'] I get: TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

This did work, but is pain to my hands and eyes:

df.['c'] = df['a']
df.['c'] = df['a'].tail(len(df.['a']-1))**2 + df.['b'].tail(len(df.['b'])-1)
df.loc[0,'c'] = 'unit for c'

Is there any way to do this quicker or with less typing? Thanks already schamonn

2

2 Answers 2

1

Let's look at the error mentioned first in this post.

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

What this error is staying that you are trying to take and string to a power, we can replicate this error using the following example:

df = pd.DataFrame({'a':['1','2','3'],'b':[4,5,6]})

df['a']**2

Output last line of stack trace:

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

A simple resolution to this if all your a column are numeric representations, then use pd.to_numeric:

pd.to_numeric(df['a'])**2

Output:

0    1
1    4
2    9
Name: a, dtype: int64

Got non-numeric strings also in column a?

Use errors = 'coerce' as a parameter to pd.to_numeric

df = pd.DataFrame({'a':['a','1','2','3'],'b':[4,5,6,7]})

Use:

pd.to_numeric(df['a'], errors='coerce')**2

Output:

0    NaN
1    1.0
2    4.0
3    9.0
Name: a, dtype: float64
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, this helps. But additionally I have the problem, that I can't do pd.to_numeric to the string in the first row of each column.
Try pd.to_numeric(df['a'], errors='coerce')**2 to force conversion of those that can convert and use NaN for values that can't convert.
@schamonn See update. You can use fillna to replace that NaN with zero of bfill ect...
Hi, this is exactly what I needed! Thx so much
0

this is how I read in the data

Data = pd.read_excel(fileName, sheet_name = 'Messung')
In [154]: Data
Out[154]: 
   T1   T2 Messung                Datum
0  °C   °C       -                    -
1  12  100       1  2018-12-06 00:00:00
2  15  200       2  2018-12-06 00:00:00
3  20  120       3  2018-12-06 00:00:00
4  10  160       4  2018-12-06 00:00:00
5  12  160       5  2018-12-06 00:00:00

1 Comment

It is recommended you modify the question and add this information to the question.

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.