Reputation: 13
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
Upvotes: 0
Views: 297
Reputation: 153460
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
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
Upvotes: 1
Reputation: 13
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
Upvotes: 0