Python Forum
pandas convert to tuple & float to float64 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: pandas convert to tuple & float to float64 (/thread-2181.html)



pandas convert to tuple & float to float64 - metalray - Feb-24-2017

Dear Pandas Experts,
I got two question on my Introduction to Python homework.

The jupiter auto-grader expects in case 1 a float64 and in case 2 a tuple, not a list.

case 1
    newfour_2['GDPDiff']=np.subtract(newfour['2015'],newfour['2006'])

    return newfour_2.iloc[0]
case 2

    purchase_1 = pd.Series({'Country': 'Brazil',
                        '% Renewable': '69.648030000000006'})
    df = pd.DataFrame([purchase_1])
    sixlist=df.to_records().tolist() 
When I used google to search for how to create tuples I only came accross tolist().
But the jupiter notebooks auto-grader expects data type "tuple"

Any help would make my weekend a great weekend! :)


RE: pandas convert to tuple & float to float64 - snippsat - Feb-24-2017

Quote:The jupiter auto-grader expects in case 1 a float64
Check types of dataframe with dtypes.
convert single value with astype
Eg:
In [27]: work_data.dtypes
Out[27]:
name         object
age           int64
weight        int64
seniority     int64
pay           int64
dtype: object

In [23]: work_data.age
Out[23]:
0    34
1    19
2    45
3    56
4    23
5    27
6    31
7    22
Name: age, dtype: int64

# To float64
In [24]:work_data.age.astype(np.float64)
Out[24]:
0    34.0
1    19.0
2    45.0
3    56.0
4    23.0
5    27.0
6    31.0
7    22.0
Name: age, dtype: float64
Quote:The jupiter auto-grader expects in case 1 a float64 and in case 2 a tuple, not a list.
Remember that you still can use Python and not only pandas.
So tuple() work.
In [26]: data = work_data.age.tolist()

print(data)
print(tuple(data))
[34, 19, 45, 56, 23, 27, 31, 22]
(34, 19, 45, 56, 23, 27, 31, 22)



RE: pandas convert to tuple & float to float64 - metalray - Feb-27-2017

Hi snippsat,
Thanks for your reply.

when I run:

    newfour_2['GDPDiff']=np.subtract(newfour['2015'],newfour['2006'])
    newfour_2['GDPDiff'].astype(np.float64)
    return type(newfour_2.iloc[0])
the output is:float not float64.


For case 2.
when I run:

    sixlist=df.to_records().tolist() 
    return type(sixlist) #newtwo["% Renewable"].max()
answer_six()
the output is:list and not tuple so tolist()does not seem to do the trick.

And to_tuple() or tuple() does not exist: AttributeError: recarray has no attribute to_tuple/tuple

Even tuple() has the type numpy.recarray

    purchase_1 = tuple({'Country': 'Brazil',
                        '% Renewable': '69.648030000000006'})

    df = pd.DataFrame([purchase_1])

                                       
    #only_gold = df.where(df['Gold']== 5)
    sixlist=df.to_records()
    return type(sixlist) #newtwo["% Renewable"].max()



RE: pandas convert to tuple & float to float64 - Ofnuts - Feb-27-2017

tuple() is a built-in function that creates a tuple from a list:

thetuple=tuple(thelist)



RE: pandas convert to tuple & float to float64 - zivoni - Feb-27-2017

1) astype(np.float64) does not change datatype in place, it returns modified dataframe/series. You need to assign it to a variable.

2) I am not entirely sure what are you trying to do, i guess that you want to get one row of dataframe as a tuple?


(Feb-27-2017, 08:55 AM)metalray Wrote:
    purchase_1 = tuple({'Country': 'Brazil',      '% Renewable': '69.648030000000006'})
    df = pd.DataFrame([purchase_1])  
This looks rather strange, with tuple from dict you are keeping only 'Country' and '% Renewable', so your final dataframe contains only these two strings.

Perhaps you want something like this?
Output:
In [67]: purchase_1 = {'Country': ['Brazil'],'% Renewable': ['69.648030000000006']} In [68]: df = pd.DataFrame(purchase_1) In [69]: df Out[69]:           % Renewable Country 0  69.648030000000006  Brazil In [70]: sixlist = df.to_records() In [71]: sixlist Out[71]: rec.array([(0, '69.648030000000006', 'Brazil')],           dtype=[('index', '<i8'), ('% Renewable', 'O'), ('Country', 'O')]) In [72]: sixlist = tuple(sixlist[0]) In [73]: sixlist Out[73]: (0, '69.648030000000006', 'Brazil') In [74]: type(sixlist) Out[74]: tuple
Or maybe this: (no index value there)
Output:
In [75]: sixlist = tuple(df.iloc[0]) In [76]: sixlist Out[76]: ('69.648030000000006', 'Brazil') In [77]: type(sixlist) Out[77]: tuple



RE: pandas convert to tuple & float to float64 - snippsat - Feb-27-2017

Quote:the output is:float not float64.
You most store it in a variable.
four_64 = newfour_2['GDPDiff'].astype(np.float64)
As mention you convert with the build tuple() after you have used tolist().
    sixlist = df.to_records().tolist() 
    return tuple(sixlist) # Will return a tuple
answer_six()



RE: pandas convert to tuple & float to float64 - metalray - Feb-27-2017

Awesome. Thanks for you help! Both of you!