pandas extract year from datetime: df[‘year’] = df[‘date’].year is not working

If you’re running a recent-ish version of pandas then you can use the datetime accessor dt to access the datetime components: In [6]: df[‘date’] = pd.to_datetime(df[‘date’]) df[‘year’], df[‘month’] = df[‘date’].dt.year, df[‘date’].dt.month df Out[6]: date Count year month 0 2010-06-30 525 2010 6 1 2010-07-30 136 2010 7 2 2010-08-31 125 2010 8 3 2010-09-30 84 … Read more

How do you extract a column from a multi-dimensional array?

>>> import numpy as np >>> A = np.array([[1,2,3,4],[5,6,7,8]]) >>> A array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> A[:,2] # returns the third columm array([3, 7]) See also: “numpy.arange” and “reshape” to allocate memory Example: (Allocating a array with shaping of matrix (3×4)) nrows = 3 ncols = 4 my_array = numpy.arange(nrows*ncols, dtype=”double”) … Read more

How to retrieve comments from within an XML Document in PHP

SimpleXML cannot handle comments, but the DOM extension can. Here’s how you can extract all the comments. You just have to adapt the XPath expression to target the node you want. $doc = new DOMDocument; $doc->loadXML( ‘<doc> <node><!– First node –></node> <node><!– Second node –></node> </doc>’ ); $xpath = new DOMXPath($doc); foreach ($xpath->query(‘//comment()’) as $comment) … Read more

Javascript: extract URLs from string (inc. querystring) and return array

I just use URI.js — makes it easy. var source = “Hello www.example.com,\n” + “http://google.com is a search engine, like http://www.bing.com\n” + “http://exämple.org/foo.html?baz=la#bumm is an IDN URL,\n” + “http://123.123.123.123/foo.html is IPv4 and ” + “http://fe80:0000:0000:0000:0204:61ff:fe9d:f156/foobar.html is IPv6.\n” + “links can also be in parens (http://example.org) ” + “or quotes »http://example.org«.”; var result = URI.withinString(source, function(url) … Read more

Extract the text out of HTML string using JavaScript

Create an element, store the HTML in it, and get its textContent: function extractContent(s) { var span = document.createElement(‘span’); span.innerHTML = s; return span.textContent || span.innerText; }; alert(extractContent(“<p>Hello</p><a href=”http://w3c.org”>W3C</a>”)); Here’s a version that allows you to have spaces between nodes, although you’d probably want that for block-level elements only: function extractContent(s, space) { var span= … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)