Python Forum
pandas convert to tuple & float to float64
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas convert to tuple & float to float64
#1
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! :)
Reply
#2
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)
Reply
#3
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()
Reply
#4
tuple() is a built-in function that creates a tuple from a list:

thetuple=tuple(thelist)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
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
Reply
#6
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()
Reply
#7
Awesome. Thanks for you help! Both of you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I convert specific rows from excel to pandas dataframe? mcva 1 1,797 Apr-20-2020, 09:14 AM
Last Post: pyzyx3qwerty
  Error could not convert string to float: deadendstreet 4 5,376 Oct-02-2019, 05:49 PM
Last Post: ichabod801
  ValueError: could not convert string to float: 'Pencil' Balakay97 3 5,893 Mar-08-2018, 07:30 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020