'DataFrame' object has no attribute 'get_value' in Pandas - python-3.x

Just learning python now, have very weak programming background.
I keep getting the error: 'DataFrame' object has no attribute 'get_value' using python 3.8.
The file is a random file I downloaded from the internet just to learn how to use dataframes and pandas. The object here is to pull a specific value out of the dataframe, so that I can manipulate it later.
import pandas as pd
pb_list = [] pb_list =
pd.read_csv(r"PB2010plus.csv") print(pb_list)
print(type(pb_list))
print(pb_list.get_value(1047, 'Winning Numbers'))
here's the error line
Traceback (most recent call last): File
"I:/Python/PycharmProjects/Learning Python 1/probabilityfunsheet.py",
line 8, in
print(pb_list.get_value(1047, 1)) File "C:\Users\greyb\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py",
line 5274, in getattr
return object.getattribute(self, name) AttributeError: 'DataFrame' object has no attribute 'get_value'
I am using pycharm, and did some searching, came across https://www.geeksforgeeks.org/python-pandas-dataframe-get_value/ which is where I got the idea as a potential solution for my 'problem'.

Starting it with an underscore worked for me
df._get_value(index,'name')

A good habit while reading data frames in Python is setting them as a variable:
import pandas as pd
pb_list = pd.read_csv("PB2010plus.csv")
Thus, to visualize them you won't need to print them, but you will just need to recall the variable pb_list.
# take a look to the dataframe
pb_list
# check the dataframe's type
type(pb_list)
# access to 1047 row index inside the Winning Numbers column
pb_list.get_value(1047, 'Winning Numbers')
However get_value has been deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead.
Regarding your question. If you want to store the value that you are searching for in a variable to manipulate it in the future, here's the code:
# storing the desired value in target_value
target_value = pb_list.get_value(1047, 'Winning Numbers')

Please try using
df._get_value()
instead of
df.get_value()

The get_values method for a DataFrame was deprecated. Use values() instead. More info here

You don't need to put the result into a list pd_list = [] This code will give you an empty list and fill out this list with for loop in general. Try to remove that code and see what happens. Hope this helps.

Related

Is it possible to query a (Python) Bokeh data source's or Pandas dataframe identifying name?

In the beginning of a bokeh plot configuration I define my dataset that is to be used for plotting:
source = ColumnDataSource(data=df_1hz_climb)
output_notebook()
Is it possible (like for adaptive title reasons) to get somehow the name of the given dataframe -in this example here it is 'df_1hz_climb'- ?
Something like
source.data.name
does not exist. (This leads to the AttributeError: 'PropertyValueColumnData' object has no attribute 'name').
And source.name is empty, resp. "None" as not beeing defined above.
As my question might be confusing I try it in a more general way. It might clarify my question:
I create a Pandas dataframe - in this example an empty one- with:
import pandas as pd
my_dataframe = pd.DataFrame()
print(my_dataframe)
Is it possible to call the identifyer of this specific dataframe, namely my_dataframe as a string?
Any hint for me?

Revit Python Shell / Revit Python Wrapper - get Element name by id

I'm trying to get the name of an element by way the ID using Revit python wrapper in Revit python shell but I'm having issues. I am typically able to do it using c# but rpw is new to me.
I try:
doc.GetElement(2161305).name or doc.GetElement(2161305).Name
and I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected Reference, got int
I've looked a bit through the docs and watched some of the videos but haven't found anything that has covered this. I'm sure its easy, I'm just not not finding the answer.
Any help / direction is appreciated.
Got to answer my own question again.
>>> from rpw import db
>>> element = db.Element(SomeElement)
>>> element = db.Element.from_id(ElementId)
>>> element = db.Element.from_int(Integer) # this one worked for me
You need to cast the integer to an ElementId. The GetElement has three overloads. None of them takes an int, so you need to cast it to clarify which one is intended. Please read the GetElement documentation.

Attempting to use choropleth maps in folium for first time, index error

This is my first time posting to stackoverflow, so I hope I am doing this correctly. I am currently finishing up a 'jumpstart' introduction to data analytics. We are utilizing Python with a few different packages, such as pandas, seaborn, folium etc. For part of our final project/presentation, I am trying to make a zipcode choropleth map. I have successfully imported folium and have my map displayed - the choropleth concept is new to me and is completely extracurricular. Trying to challenge myself to succeed.
I found an example of creating a choropleth map here that I am trying to use: https://medium.com/#saidakbarp/interactive-map-visualization-with-folium-in-python-2e95544d8d9b. I believe I correctly substituted the different object names for the data frame and map name that I am working with. For the geoJSON data, I found this https://github.com/OpenDataDE/State-zip-code-GeoJSON. I opened the geoJSON file in Atom, and found the feature title for what I believe to be the five digit zipcode 'ZCTA5CE10'.
Here is my code:
folium.Choropleth(geo_data='../data/tn_tennessee_zip_codes_geo.min.json',
data=slow_to_resolve,
columns=['zipcode'],
key_on='feature.properties.ZCTA5CE10',
fill_color='BuPu', fill_opacity=0.7, line_opacity=0.2,
legend_name='Zipcode').add_to(nash_map)
folium.LayerControl().add_to(nash_map)
nash_map
When I try to run the code, I get this error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-114-a2968de30f1b> in <module>
----> 1 folium.Choropleth(geo_data='../data/tn_tennessee_zip_codes_geo.min.json',
2 data=slow_to_resolve,
3 columns=['zipcode'],
4 key_on='feature.properties.ZCTA5CE10',
5 fill_color='BuPu', fill_opacity=0.7, line_opacity=0.2,
~\anaconda3\lib\site-packages\folium\features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
1198 if hasattr(data, 'set_index'):
1199 # This is a pd.DataFrame
-> 1200 color_data = data.set_index(columns[0])[columns[1]].to_dict()
1201 elif hasattr(data, 'to_dict'):
1202 # This is a pd.Series
IndexError: list index out of range
Prior to this error, I had two columns from my dataframe specified, but I got some 'isnan' error that I am pretty sure was attributed to string type data in the second column, so I removed it. Now currently trying to figure out this posted error.
Can someone point me in the right direction? Please keep in mind that aside from this three week jumpstart program, I have zero programming knowledge or experience - so I am still learning terminology and concepts.
Thank you!
The error you got was an IndexError: list index out of range because you provided to the columns parameter with just one column columns=['zipcode']. It has to be two like this: columns=['zipcode', 'columnName_to_color_map'].
Where the first column 'zipcode' must match the object node in the GeoJSON file data (note the format/type must also match, that is string: '11372' IS NOT integer: 11372). The second column 'columnName_to_color_map' or whatever name you used should be the column which defined the choropleth colors.
Also note that key_on should match the first column 'zipcode'.
So the code should look like this:-
folium.Choropleth(geo_data='../data/tn_tennessee_zip_codes_geo.min.json',
data=slow_to_resolve,
columns=['zipcode', 'columnName_to_color_map'],
key_on='feature.properties.ZCTA5CE10',
fill_color='BuPu', fill_opacity=0.7, line_opacity=0.2,
legend_name='Zipcode').add_to(nash_map)
folium.LayerControl().add_to(nash_map)
nash_map

How to import .dta via pandas and describe data?

I am new to python and have a simple problem. In a first step, I want to load some sample data I created in Stata. In a second step, I would like to describe the data in python - that is, I'd like a list of the imported variable names. So far I've done this:
from pandas.io.stata import StataReader
reader = StataReader('sample_data.dta')
data = reader.data()
dir()
I get the following error:
anaconda/lib/python3.5/site-packages/pandas/io/stata.py:1375: UserWarning: 'data' is deprecated, use 'read' instead
warnings.warn("'data' is deprecated, use 'read' instead")
What does it mean and how can I resolve the issue? And, is dir() the right way to get an understanding of what variables I have in the data?
Using pandas.io.stata.StataReader.data to read from a stata file has been deprecated in pandas 0.18.1 version and hence you are getting that warning.
Instead, you must use pandas.read_stata to read the file as shown:
df = pd.read_stata('sample_data.dta')
df.dtypes ## Return the dtypes in this object
Sometimes this did not work for me especially when the dataset is large. So the thing I propose here is 2 steps (Stata and Python)
In Stata write the following commands:
export excel Cevdet.xlsx, firstrow(variables)
and to copy the variable labels write the following
describe, replace
list
export excel using myfile.xlsx, replace first(var)
restore
this will generate for you two files Cevdet.xlsx and myfile.xlsx
Now you go to your jupyter notebook
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel('Cevdet.xlsx')
This will allow you to read both files into jupyter (python 3)
My advice is to save this data file (especially if it is big)
df.to_pickle('Cevdet')
The next time you open jupyter you can simply run
df=pd.read_pickle("Cevdet")

How do I get a list from set in python 3

This seems to work in python 2.7, but not python 3. Is there an easy way to make a set a list in python 3 that I am missing? Thanks in advance.
mylist = [1,2,3,4,5]
list(set(mylist))
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: 'list' object is not callable
Sorry if this has been asked before, I did a quick search and didn't see an answer specific to python3.
list(set(...)) works fine. The error indicates the 3.x version of the code has a variable called list or set, shadowing the built-in function. Perhaps you renamed mylist to list? Rest assured, that mistake would provoke the exact same error message in Python 2.

Resources