I am working on a data analysis project and I am trying to order my results in descending format, the first time I had a similar issue, I used sorted(dictt.items(), key= lambda x: x[1]) and it worked fine. Now, I am having an error of type: "AttributeError: 'set' object has no attribute 'items'".
What am I doing wrong?
Actual output but I want it sorted
There is a special collection for what you are trying to do called OrderedDict:
https://docs.python.org/3/library/collections.html#collections.OrderedDict
The problem is that sorting the way you did returns a set of items instead of a dict and you have to turn it into a dict again. However, the standard dict doesn't keep track of entry order for efficiency reasons, so if you really want that, just plug the set of items into an OrderedDict like so:
myOrderedDict = OrderedDict(sorted(dictt.items()))
Related
I'm studying ANSYS pyMAPDL, following the tutorial https://www.youtube.com/watch?v=szHmg-xW_hM&t=1s
It was understood from both the tutorial code itself and pyMAPDL documentation that prnsol returns a str object, but it has the enriched method to_list() which allows the output to be exported to list format. https://mapdldocs.pyansys.com/user_guide/post.html
However, when I run the following line from the sample code, it just reports attribute error
mapdl_s_1_list = mapdl.prnsol('S', 'PRIN',).to_list()
AttributeError: 'str' object has no attribute 'to_list'
If I print out the prnsol() result, it contains both the header and table, I have to do some processing in order to extra the table information for further processing.
Would like to know if there is a quick fix to make the enriched method work? Thanks!
hawkoli1987
This problem seems very simple, but I'm having trouble finding an already existing solution on StackOverflow.
When I run a sqlalchemy command like the following
valid_columns = db.session.query(CrmLabels).filter_by(user_id=client_id).first()
I get back a CrmLabels object that is not iterable. If I print this object, I get a list
[Convert Source, Convert Medium, Landing Page]
But this is not iterable. I would like to get exactly what I've shown above, except as a list of strings
['Convert Source', 'Convert Medium', 'Landing Page']
How can I run a query that will return this result?
Below change should do it:
valid_columns = (
db.session.query(CrmLabels).filter_by(user_id=client_id)
.statement.execute() # just add this
.first()
)
However, you need to be certain about the order of columns, and you can use valid_columns.keys() to make sure the values are in the expected order.
Alternatively, you can create a dictionary using dict(valid_columns.items()).
I created this function:
def designate(people,race):
global i2
global clilaw
i2=i2.sort_values(by=people)
i2=pd.merge(i2,clilaw,how='left',left_on=people,right_on='category')
i2[race]=i2['race']
When I feed in values individually, it works.
designate(prl[1],race2[1])
designate(prl[2],race2[2])
...
designate(prl[12],race2[12])
But when I try to create a loop to do it, like this:
for k in range(len(prl)):
designate(prl[k],race2[k])
I get a key error:
KeyError: 'race'
range(len(prl)) is [0, 1, \dots, len(prl)-1.
In the for loop you are trying to access designate(prl[0], race2[0]) and so on but when you feed the values manually you start from 1. Could that be the problem? If so try range(1, len(prl)+1).
I understand now. Each time the merging is done, different race columns (race_x, race_y) are created so during the next iteration, i2['race'] isn't recognized.
Create a function that concatenates two dataframes. Use previously created function to create
two dataframes and pass them as parameters Make sure that the indexes are reset before
returning:
I am importing a Data set from quandl using API. Everything is perfect, however the time series I am importing is reversed. By this I mean if I used the .head method to print the first elements in the data set, I will get the latest Data set figures and printing the tail will get oldest figures
df = pd.read_csv("https://www.quandl.com/api/v3/datasets/CHRIS/CME_CD4.csv?api_key=H32H8imfVNVm9fcEX6kB",parse_dates=['Date'],index_col='Date')
df.head()
This should be a pretty easy fix if I understand. Credit to behzad.nouri on this answer Right way to reverse pandas.DataFrame?.
You just need to reverse the order of your dataframe using the line below.
df = df.reindex(index=df.index[::-1])
I'm trying to scrape the MTA website and need a little help scraping the "Train Lines Row." (Website for reference: https://advisory.mtanyct.info/EEoutage/EEOutageReport.aspx?StationID=All
The train line information is stored as image files (1 line subway, A line subway, etc) describing each line that's accessible through a particular station. I've had success scraping info out of rows in which only one train passes through, but I'm having difficulty figuring out how to iterate through the columns which have multiple trains passing through it...using a conditional statement to test for whether it has one line or multiple lines.
tableElements = table.find_elements_by_tag_name('tr')
that's the table i'm iterating through
tableElements[2].find_elements_by_tag_name('td')[1].find_element_by_tag_name('h4').find_element_by_tag_name('img').get_attribute('alt')
this successfully gives me the values if only one value exists in the particular column
tableElements[8].find_elements_by_tag_name('td')[1].find_element_by_tag_name('h4').find_elements_by_tag_name('img')
this successfully gives me a list of values I can successfully iterate through to extract my needed values.
Now I try and combine these lines of code together in a forloop to extract all the information without stopping.
for info in tableElements[1:]:
if info.find_elements_by_tag_name('td')[1].find_element_by_tag_name('h4').find_elements_by_tag_name('img')[1] == True:
for images in info.find_elements_by_tag_name('td')[1].find_element_by_tag_name('h4').find_elements_by_tag_name('img'):
print(images.get_attribute('alt'))
else:
print(info.find_elements_by_tag_name('td')[1].find_element_by_tag_name('h4').find_element_by_tag_name('img').get_attribute('alt'))
I'm getting the error message: "list index out of range." I dont know why, as every iteration done in isolation seems to work. My hunch is I haven't correctly used the boolean operation properly here. My idea was that if find_elements_by_tag_name had an index of [1] that would mean multiple image text for me to iterate through. Hence, why I want to use this boolean operation.
Hi All, thanks so much for your help. I've uploaded my full code to Github and attached a link for your reference: https://github.com/tsp2123/MTA-Scraping/blob/master/MTA.ElevatorData.ipynb
The end goal is going to be to put this information into a dataframe using some formulation of and having a for loop that will extract the image information that I want.
dataframe = []
for elements in tableElements:
row = {}
columnName1 = find_element_by_class_name('td')
..
Your logic isn't off here.
"My hunch is I haven't correctly used the boolean operation properly here. My idea was that if find_elements_by_tag_name had an index of [1] that would mean multiple image text for me to iterate through."
The problem is it can't check if the statement is True if there's nothing in index position [1]. Hence the error at this point.
if info.find_elements_by_tag_name('td')[1].find_element_by_tag_name('h4').find_elements_by_tag_name('img')[1] == True:
What you want to do is use try: So something like:
for info in tableElements[1:]:
try:
if info.find_elements_by_tag_name('td')[1].find_element_by_tag_name('h4').find_elements_by_tag_name('img')[1] == True:
for images in info.find_elements_by_tag_name('td')[1].find_element_by_tag_name('h4').find_elements_by_tag_name('img'):
print(images.get_attribute('alt'))
else:
print(info.find_elements_by_tag_name('td')[1].find_element_by_tag_name('h4').find_element_by_tag_name('img').get_attribute('alt'))
except:
#do something else
print ('Nothing found in index position.')
Is it also possible to back to your question and provide the full code? When I try this, I'm getting 11 table elements, so want to test it with the specific table you're trying to scrape.