This question already has answers here:
Change the number of lines shown in Visual Studio Code's built-in Terminal
(3 answers)
Closed 2 years ago.
I want to inspect all unique values in a column in a pandas dataframe, however, using df.column.unique() only gives the starting and ending values, and values in the middle are hidden with an ellipsis.
I tried
mylist = list(df.column.unique())
mylist
which showed more values but not till the end.
Edit:
mylist ouput looks like this:
['PSPC000',
'LEV12345RTC',
'LV150390XYZ',
'WPX-100',
'FSM-Y2222',
'FM-YX3',
'ELB1100',
'Lx145BP',
'CE503pxp',
'Exxy351',
...]
Try this:
print (df.column.unique().tolist())
Related
This question already has answers here:
Pandas: Setting no. of max rows
(10 answers)
Closed 1 year ago.
I am working with python 3 and the pandas package in visual studio code and I the print() function is not displaying correctly.
For example when I am using df.head() it looks good.
But If I use the print() statement I no longer see all of the columns next to each other, some of them get dragged down for some reason. And I can't see the entire data
Anyone knows what I can do to see the entire data, and all of the columns next to each other?
The problem comes from library pandas that cuts part of your dataframe when it's too long. Before your print, add this line:
pandas.set_option('max_row', None)
to display the entier row.
Also, you will be able to see all your data adding None argument in head():
trading.head(None)
This question already has answers here:
How do I create variable variables?
(17 answers)
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
generating variable names on fly in python [duplicate]
(6 answers)
Closed 3 years ago.
I have a ticker and I want to check a specific list of tickers to see if the ticker is found. If it is found, it will replace it.
The new tickers come from another data source and therefore do not know which specific list of tickers to check. In order to find that list, I can pass the lists name as a string but upon iterating the code (naturally) recognizes this as string as opposed to a list to iterate.
Is there a way to have the code/function recognize that the string is actually a specific list to be checked? In reading other questions, I know this may not be possible...in that case what is an alternative?
list_1=['A','B']
list_2=['C','D']
old_ticker='A'
new_ticker='E'
assigned_list='list_1'
def replace_ticker(old_ticker,new_ticker,list):
for ticker in list:
if new_ticker in list:
return
else:
list.append(new_ticker)
list.remove(old_ticker)
replace_ticker(old_ticker,new_ticker,assigned_list)
You key the needed lists by name in a dictionary:
ticker_directory = {
"list_1": list_1,
"list_2": list_2
}
Now you can accept the name and get the desired list as ticker_directory[assigned_list].
list_1=['A','B']
list_2=['C','D']
lists = {
'list_1':list_1,
'list_2':list_2
}
old_ticker='A'
new_ticker='E'
assigned_list='list_1'
def replace_ticker(old_ticker,new_ticker,list_name):
if old_ticker not in lists[list_name]:
return
else:
lists[list_name].append(new_ticker)
lists[list_name].remove(old_ticker)
replace_ticker(old_ticker,new_ticker,assigned_list)
print(lists[assigned_list])
This is the complete program from what i perceived.
#prune already answered this, I have just given the whole solution
There are at least two possibilities:
1 As noted in comments kind of overkill but possible:
Use eval() to evaluate string as python expressions more in the link:
https://thepythonguru.com/python-builtin-functions/eval/
For example:
list_name = 'list_1'
eval('{}.append(new_ticker)'.format(list_name))
2 Second
Using locals() a dictionary of locally scoped variables similiar to the other answers but without the need of creating the dict by hand which also requires the knowledge of all variables names.
list_name = 'list_1'
locals()[list_name].append(new_ticker)
This question already has answers here:
How can I display full (non-truncated) dataframe information in HTML when converting from Pandas dataframe to HTML?
(10 answers)
Closed 3 years ago.
I want to show content of a dataframe that I created. The problem is that it shows only part of the column content:
Is there an option to see all the columns' content?
You can try using dataframe.head(n=100). Just change the value of n to how many items you want to display
This question already has answers here:
Python Argument Binders
(7 answers)
Closed 8 months ago.
As you can see from the code below, I'm adding a series of functions to a list.
The result is that each function gets ran and the returned value is added to the list.
foo_list = []
foo_list.append(bar.func1(100))
foo_list.append(bar.func2([7,7,7,9]))
foo_list.append(bar.func3(r'C:\Users\user\desktop\output'))
What I would like to know is, is it possible to have the function stored in the list and then ran when it is iterated upon in a for loop?
Yeah just use lambda:
foo_list = []
foo_list.append(lambda: bar.func1(100))
foo_list.append(lambda: bar.func2([7,7,7,9]))
foo_list.append(lambda: bar.func3(r'C:\Users\user\desktop\output'))
for foo in foo_list:
print(foo())
This question already has answers here:
How to remove an element from a list by index
(18 answers)
Closed 8 years ago.
I'm writing a statistics program which will maintain a list of float values. The program will implement the following menu:
Add value to list
Delete value from list (by value)
Delete value from list (by location in list)
Display list
Exit
I've written everything except the third option. I can't figure out how to get it done. How would one do that?
In Python you can delete an item from a list by referencing it. Like this:
del list[location]