How to show all items of a list in Python? - python

I have around 10K rows in a dataframe. I want to copy-paste all items then apply them all to my SQL query as my filter.
Here is how I convert to a list
list_a = df['col'].to_list()
When I tried to print out the list it only shows me 1k of the items. I tried to set pd.set_option('display.max_rows', None) but it seems doesn't work. It only works for pandas dataframe.
My output is,
>>> list_a
>>> ['a1',
'a2',
'a3',
till 'a1000' then it ignore the rest of 9k with
...
]
Can I get all 10k items print out?

As #Barmar points out in their comments, this print issue is not related to pandas because the pandas.series.to_list() function returns a list object.
Also #Thornily is stating that this is related to your python console trying to conserve resources. The reason python uses summarization (...) is because printing an excessive number of lines is slow and will be very difficult to visual read through the output.
Finally, I think #BENY's comment can probably help provide insight into why you need to print this many. Like #Drakax has pointed out, you can use len(list_a) to verify the list has all 10,000 items in it.
I am assuming that the OP doesn't actually need to read all 10,000 records and that they likely want to know WHY they can't override the summarization (...). Unfortunately, I am still working on the WHY answer. I have tried changing the default preferences in my console and using numpy.set_printoptions(threshold=sys.maxsize) but I still get the elipsis. I will keep trying to answer the WHY question and will update this post if I find a good answer.
For now, IF you want a HOW answer, you can use the very slow method below. Keep in mind, I do not recommend printing the output to your console for the reasons stated above. But if you ABSOLUTELY have to print to your console you can do this:
for n in list_a:
print(n)
or you can also convert using the list function to force the object to a python list and print multiple values on each console line:
list_a = list(list_a)
print(list_a)

Related

Remove spaces from strings in pandas DataFrame not working

Trying to remove spaces from a column of strings in pandas dataframe. Successfully did it using this method in other section of code.
for index, row in summ.iterrows():
row['TeamName'] = row['TeamName'].replace(" ", "")
summ.head() shows no change made to the column of strings after this operation, however no error as well.
I have no idea why this issue is happening considering I used this exact same method later in the code and accomplished the task successfully.
Why not use str.replace:
df["TeamName"] = df["TeamName"].str.replace(r' ', '', regex=False)
I may be proven wrong here, but I am wondering if its because you are iterating over it, and maybe working on a copy that isn't changing the data. From pandas.DataFrame.iterrows documentation, this is what I found there:
"You should never modify something you are iterating over. This is not guaranteed to work in all cases. Depending on the data types, the iterator returns a copy and not a view, and writing to it will have no effect."
just a thought... hth

how apply conditional statement on sql query result in python

In python , i want to apply following if condition on mysql result. but it is not able to compare the result, what i do??
In this code there is table named as 'mansih' but still it's not printing 'hello'.
dir=ptr.execute('show tables')
for i in ptr.fetchall():
print(i)
if i== 'mansih':
print('hello')
the output of this code just printing the result of print(i). output is
('3432fddf',)
('dgdfdf232342334243432',)
('man456',)
('mansih',)
here i expect to print hello but it's not printed. so please provide any solution using which i can check whether given touple exist in database or not.
As you can see in your comment:
('3432fddf',) ('dgdfdf232342334243432',) ('man456',) ('mansih',)
You get tuples, and not strings, from fetchall. It should work if you modify it to if i[0] == 'mansih':, so as to fetch the first element of the tuple – which is the string you expect.
Two important lessons from this are:
You can use the (interactive, optionally) interpreter to test your outputs, and you should make sure that you understand what you see. This could spare you lots of head scratching.
Take a look at documentation if something doesn't work as expected, to make sure you understand how your tools work.

Finding a set of values

I have a curve_hex generator, it uses two coordinates, I'm looking for only one coordinate. The script searches for one coordinate value easily, but if I specify several coordinates for the search, then it does not find anything.
wanted='58611774815559422402170859520215717661755632997646071327165159211728464937238'
curve_hex='(58611774815559422402170859520215717661755632997646071327165159211728464937238, 108722706890170119196943746054760504186165603293283329661416022207913727808252)'
if str(curve_hex).find(wanted)!=-1:
print (curve_hex[str(curve_hex).find(wanted):str(curve_hex).find(wanted)+len(wanted)])
else:
print ('not')
One value is found normally. But if I add several values, the script writes an error
wanted='58611774815559422402170859520215717661755632997646071327165159211728464937238', '108722706890170119196943746054760504186165603293283329661416022207913727808252'
curve_hex='(58611774815559422402170859520215717661755632997646071327165159211728464937238, 108722706890170119196943746054760504186165603293283329661416022207913727808252)'
if str(curve_hex).find(wanted)!=-1:
print (curve_hex[str(curve_hex).find(wanted):str(curve_hex).find(wanted)+len(wanted)])
else:
print ('not')
Tell me how to do it right. What am I doing wrong. I have just started learning python.
Very exciting that you are learning python, I would also suggest that you might want to spend some time in the stackoverflow section explaining how to ask a question because I am not 100% sure what you are trying to achieve with your code.
From my understanding, if you are happy with your if else conditions, and your only problem is that you can’t add multiple values to wanted. I would convert wanted to a list that includes all your wanted values, and loop over those.
Something like this:
wanted = ['586..{copy the whole value here}', '108...{copy the value here}']
curve_hex='(58611774815559422402170859520215717661755632997646071327165159211728464937238, 108722706890170119196943746054760504186165603293283329661416022207913727808252)'
for value in wanted:
if str(curve_hex).find(value)!=-1:
print (curve_hex[str(curve_hex).find(value):str(curve_hex).find(value)+len(value)])
else:
print ('not')
Edit: formatting and typos
You are misleadiong some basic concepts:
Difinitions like this result in diferent types
wanted='58611774815559422402170859520215717661755632997646071327165159211728464937238'
type(wanted) # string
wanted='58611774815559422402170859520215717661755632997646071327165159211728464937238', '108722706890170119196943746054760504186165603293283329661416022207913727808252'
type(wanted) # tuple
curve_hex='(58611774815559422402170859520215717661755632997646071327165159211728464937238, 108722706890170119196943746054760504186165603293283329661416022207913727808252)'
type(wanted) # string
So you should choose a type first. Tuple is the best case.
wanted=('58611774815559422402170859520215717661755632997646071327165159211728464937238', '108722706890170119196943746054760504186165603293283329661416022207913727808252')
curve_hex=('58611774815559422402170859520215717661755632997646071327165159211728464937238', '108722706890170119196943746054760504186165603293283329661416022207913727808252')
for i in wanted:
for x,j in enumerate(curve_hex):
if i in j:
print(f'{i} in {curve_hex} at position {x}')

Content info of 'row' in ( E.g for index, row in df.iterrows() ) for pandas

There is this code commonly used in python pandas "for index, row in df.iterrows()".
What is the difference between displaying these during the loop:
print(row)
print(row.index)
print(row.index[index])
print(row[index])
I tried printing them and cant comprehend what it does and how it selects the content and I cant find a well explained source online.
For one, it's more concise.
Secondly, you're only supposed to use it for displaying data rather than modifying. According to the docs you may get unpredictable results (a concurrent modification thing, methinks).
As to how it selects it, the docs also say it just returns the individual rows as pd.Series with the index being the id pandas uses to keep track of each row in the pd.DataFrame. I'd guess it'd be a akin to using a python zip() function on a list of int [0..n] and a list of pd.Series.

how to pass list values through url

I have written a python script where I have collected some values in a list. I need to pass on these values to an URL in a loop where in each time a different value is picked up.
i..e, I want to achieve this:
http://www.abc.com/xyz/pqr/symbol=something[i].
Here "something" is a list and I have verified that it contains the proper values. However when I pass the values to the URL, I am not getting the desired results. I have tried with URL encoding for something[i] but still it is not giving me proper results. Can someone help me?
EDIT: My example script at the moment is:
import json
script=["Linux","Windows"]
for i in xrange(len(script)):
site="abc.com/pqr/xyz/symbol=json.dumps(script[i])";
print site
I think the problem is your approach to formatting. You don't really need json if you have a list already and are just trying to modify a URL...
import json
script=["Linux","Windows"]
something = ["first","second"]
for i,j in zip(script,something):
site="http:abc.com/pqr/xyz/symbol={0}".format(j)
print i, site
This uses the .format() operator, which "sends" the values in parentheses into the string at the positions marked with {}. You could just add the strings together if it is always at the end. You could also use the older % operator instead. It does pretty much the same thing, but in this case it inserts the string j at the position marked by %s:
site="http:abc.com/pqr/xyz/symbol=%s" % (j)
Side note: I slightly prefer % because once you learn it, it can also be used in other programming languages, but .format() has more options and is the recommended way to do it since python 2.6.
Output:
Linux http:abc.com/pqr/xyz/symbol=first
Windows http:abc.com/pqr/xyz/symbol=second
You should be able to get close to what you want from this starting point, but if this is nothing like your desired output, then you need to clarify in your question...

Categories