plotting many graph with matplolib (bargraph) - matplotlib

I have , dictionary
st={'Google Phone': [318, 379, 463, 580, 479, 391, 411, 359, 341, 597, 493, 714],
'iPhone': [381, 439, 537, 691, 640, 533, 501, 439, 396, 715, 664, 906],
'Vareebadd Phone': [125, 129,174,220,185,155,146,141,126,203,177,284]}
where all values in list are in following order:
Vareebadd Phone<Google phone<iphone
like---->381>318>125 || 439>379>129 for all values with same index
i got :
now if I put some change in
st={'Google Phone': [318, 379, 463, 580, 479, 391, 411, 359, 341, 597, 493, 714],
'iPhone': [381, 439, 537, 691, 640, 533, 501, 439, 396, 715, 664, 906],
'Vareebadd Phone': [125, 129,174,220,185,155,146,141,126,203,177,910]}
i got this:
my code is:
monthsname=['Jan','Feb','Mar','Apr','May','Jun','July','Aug','Sep','Oct','Nov','Dec']
plt.figure(figsize=(8,5))
plt.xticks(xpos,monthsname)
plt.bar(monthsname,st['iPhone'],w,label='iPhone')
plt.bar(monthsname,st['Google Phone'],w,label='Google Phone')
plt.bar(monthsname,st['Vareebadd Phone'],w,label='Vareebadd Phone')
# plt.figure(figsize=(10,5))
plt.legend()
plt.show()

If you are open to other packages, consider Pandas:
import pandas as pd
pd.DataFrame(st,index=monthsname ).plot.bar()
Output:
Or for the second data frame:
Is it what you are looking for?

Related

Delete ranges of id's

I have this huge SQL query which I would like to reduce:
DELETE
FROM users
WHERE id in (754, 755, 756, 757, 758, 759, 795, 796, 797, 798, 799, 765,
766, 767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794);
I tried:
DELETE
FROM users
WHERE id => 754 AND id <= 759
AND id => 795 AND id <= 799
AND id => 765 AND id <= 794;
But I get error:
ERROR: syntax error at or near "=>"
Position: 42
Do you know how I can fix this issue?
You could do it this way:
DELETE
FROM users
WHERE
id between 754 and 759
OR id between 765 and 799
use >= operator
example
delete from users where id >= 754

I'm trying to import CSV file using pandas, But I'm getting Error

C:\Users\Misti\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Misti/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
File "C:/Users/Misti/PycharmProjects/pythonProject/main.py", line 2, in <module>
df = pd.read_csv('milestone2.csv')
File "C:\Users\Misti\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:\Users\Misti\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\readers.py", line 586, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\Misti\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\readers.py", line 482, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\Misti\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\readers.py", line 811, in __init__
self._engine = self._make_engine(self.engine)
File "C:\Users\Misti\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\readers.py", line 1040, in _make_engine
return mapping[engine](self.f, **self.options) # type: ignore[call-arg]
File "C:\Users\Misti\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 69, in __init__
self._reader = parsers.TextReader(self.handles.handle, **kwds)
File "pandas\_libs\parsers.pyx", line 549, in pandas._libs.parsers.TextReader.__cinit__
pandas.errors.EmptyDataError: No columns to parse from file
Process finished with exit code 1

Julia and dbscan clustering: how extract elements from resulting structure?

Warning: this is from a julia n00b!
After performing dbscan on a point coordinate array in Julia. (Note that this is not the 'distance based method' that returns 'assignments' as part of the result structure, but the 'adjacency list' method). Documentation here. I attempt to access the vector containing the indices, but I am at a loss when trying to retrieve the members of individual clusters:
dbr = dbscan(pointcoordinates, .1, min_neighbors = 10, min_cluster_size = 10)
13-element Array{DbscanCluster,1}:
DbscanCluster(17, [4, 12, 84, 90, 94, 675, 676, 737, 873, 965], [27, 108, 177, 880, 954, 1050, 1067])
DbscanCluster(10, Int64[], [46, 48, 51, 57, 188, 225, 226, 228, 270, 542])
DbscanCluster(11, [48, 51, 228], [46, 49, 57, 188, 225, 226, 270, 542])
DbscanCluster(14, [418, 759, 832, 988, 1046], [830, 831, 855, 865, 989, 991, 996, 1021, 1070])
DbscanCluster(10, Int64[], [624, 654, 664, 803, 805, 821, 859, 987, 1057, 1069])
It is easy to retrieve a single cluster from the array:
> dbr[1]
DbscanCluster(17, [4, 12, 84, 90, 94, 675, 676, 737, 873, 965], [27, 108, 177, 880, 954, 1050, 1067])
But how do i get the stuff inside DBscanCluster?
a = dbr[1]
DbscanCluster(17, [4, 12, 84, 90, 94, 675, 676, 737, 873, 965], [27, 108, 177, 880, 954, 1050, 1067])
In [258]:
a[1]
MethodError: no method matching getindex(::DbscanCluster, ::Int64)
Thank you for your help, and sorry if I am missing something glaring!
What makes you say that DbscanCluster is a child of array?
julia> DbscanCluster <: AbstractArray
false
You might be confused by Array{DbscanCluster,1} in your result, but this just tells you that the object returned by the dbscan call is an Array the elements of which are of type DbscanCluster - this does not tell you anything about whether those elements themselves are subtypes of Array.
As for how to get the indexes, the docs for DbscanResult show that the type has three fields:
seeds::Vector{Int}: indices of cluster starting points
assignments::Vector{Int}: vector of clusters indices, where each point was assigned to
counts::Vector{Int}: cluster sizes (number of assigned points)
each of which you can access with dot notation by doing e.g. drb[1].assignments.
If you want to get say the counts for all the 13 clusters in your results, you can broadcast getproperty like so:
getproperty.(drb, :counts)
Note that counts does not exist for in the case of the "adjacency lists" method of dbscan, one can use:
getproperty.(drb, :core_indices)

Access dataframe with multi-level index on pandas v1.0.3

I'm trying to access a row of the dataframe dtSortedTable by
dtSortedTable.loc[decisionCountSorted.index[0]]
dtSortedTable is
X0 X1 X2
(D1, G2) A B C
(D2, G1) A A A
(D2, G0) A A C
decisionCountSorted indexes look like:
Index([('D1', 'G2'), ('D2', 'G1'), ('D2', 'G0')], dtype='object')
The indexes of decisionCountSorted are exactly the same as dtSortedTable. The indexes are multilevel with 2 levels. Why am I getting the below error? I need to run some tests on decisionCountSorted and extract the corresponding rows from dtSortedTable. Any help would be hugely appreciated!
Traceback (most recent call last):
File "/usr/local/Caskroom/miniconda/base/envs/logicsim/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'D1'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/tkazi/Documents/Code/logicsim/logic.py", line 183, in <module>
dtFixed = dtConsensus(dtSortedTable,quorumCount)
File "/Users/tkazi/Documents/Code/logicsim/logic.py", line 119, in dtConsensus
print(dtSortedTable.loc[decisionCountSorted.index[0]])
File "/usr/local/Caskroom/miniconda/base/envs/logicsim/lib/python3.8/site-packages/pandas/core/indexing.py", line 1762, in __getitem__
return self._getitem_tuple(key)
File "/usr/local/Caskroom/miniconda/base/envs/logicsim/lib/python3.8/site-packages/pandas/core/indexing.py", line 1272, in _getitem_tuple
return self._getitem_lowerdim(tup)
File "/usr/local/Caskroom/miniconda/base/envs/logicsim/lib/python3.8/site-packages/pandas/core/indexing.py", line 1389, in _getitem_lowerdim
section = self._getitem_axis(key, axis=i)
File "/usr/local/Caskroom/miniconda/base/envs/logicsim/lib/python3.8/site-packages/pandas/core/indexing.py", line 1965, in _getitem_axis
return self._get_label(key, axis=axis)
File "/usr/local/Caskroom/miniconda/base/envs/logicsim/lib/python3.8/site-packages/pandas/core/indexing.py", line 625, in _get_label
return self.obj._xs(label, axis=axis)
File "/usr/local/Caskroom/miniconda/base/envs/logicsim/lib/python3.8/site-packages/pandas/core/generic.py", line 3537, in xs
loc = self.index.get_loc(key)
File "/usr/local/Caskroom/miniconda/base/envs/logicsim/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'D1'

Postgresql Grouping Error refers to a column that was never referenced in the query

I have a model "Playlist" which has_many and belongs_to another model "User", through an intermediary model "PlaylistUser". "Playlist" has several attributes including id, name, and subject_id.
I'm trying to get this query to work:
#playlist_ids = #playlist.user_ids
#more = Playlist.select('playlists.id, playlists.name')
.joins(:playlist_users)
.where('playlists.id NOT IN (30, 41)')
.where(playlist_users: {user_id: #playlist_ids})
.group('playlists.id, playlists.name')
.order('count(*) desc')
which, for a given Playlist #playlist, is supposed to list all the other Playlists that share a User with #playlist, ordered by how many they share.
It works on Postgres 9.4, but with Postgres 8.4 it returns this error:
PG::GroupingError: ERROR: column "playlists.subject_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "playlists".* FROM "playlists" INNER JOIN "playlist...
^
: SELECT "playlists".* FROM "playlists" INNER JOIN "playlist_users" ON "playlist_users"."playlist_id" = "playlists"."id" INNER JOIN "users" ON "users"."id" = "playlist_users"."user_id" WHERE (NOT (playlists.id = 30 OR playlists.id = 41)) AND "users"."id" IN (45, 89, 71, 117, 115, 173, 177, 180, 161, 220, 223, 199, 221, 239, 204, 205, 206, 207, 211, 261, 282, 284, 286, 251, 252, 255, 310, 311, 315, 318, 307, 362, 319, 306, 289, 316, 305, 321, 322, 330, 333, 292, 294, 304, 300, 340, 341, 342, 343, 405, 406, 410, 408, 409, 407, 413, 416, 417, 418, 425, 427, 392, 401, 403, 445, 446, 449, 450, 379, 456, 451, 454, 459, 437, 442, 444, 496, 501, 518, 548, 549, 533, 553, 1112, 1113, 1459, 455, 348, 1458, 242, 1275, 151, 1890, 336, 203, 404, 166, 453, 114, 157, 285, 448, 447, 443, 550, 2167, 2168, 287, 320, 293, 65, 2098, 2097, 2099, 387, 3, 2175, 2170, 2174, 2182, 2171, 438, 2180, 2181, 2169, 2176, 347, 2429, 2177, 2445, 2178, 2447, 58, 2480, 390, 452, 554, 555, 313, 92, 275, 335, 428, 167, 302, 2173, 1538) GROUP BY playlists.id, playlists.name ORDER BY count(*) desc
But I'm not referencing the column subject_id anywhere in my query, or anywhere in the view, controller, or model for the page I'm on.
Why would that column have anything to do with whether my query works or not?
Assocations:
class Playlist < ActiveRecord::Base
has_many :playlist_users
has_many :users, :through => :playlist_users
end
class PlaylistUser < ActiveRecord::Base
belongs_to :playlist
belongs_to :user
end
class User < ActiveRecord::Base
has_many :playlist_users
has_many :playlists, :through => :playlist_users
end
Maybe you are not referencing subject_id explicitly but your result query uses:
SELECT "playlists".* FROM "playlists"
And because of the wildcard * you are getting all of the columns in playlist, which includes playlists.subject_id.
You will have to add that column to the group_by clause as well as any others that are grabbed by the wildcard SELECT.