I am trying to concatenate each string element of a pandas Series object with '.NS'. My sample code is as follows:
import pandas as pd
asSeries = pd.Series(['HCLTECH', 'ADANIPORTS', 'HDFC', 'TECHM', 'ONGC', 'DIVISLAB', 'TCS'])
To concatenate each element with .NS, I am using the following code:
asTextWithNS = asSeries.str.cat(sep='.NS ')
print(asTextWithNS)
The result is as follows:
HCLTECH.NS ADANIPORTS.NS HDFC.NS TECHM.NS ONGC.NS DIVISLAB.NS TCS
As you can see, the last element is not concatenated with .NS, and I have checked with many other longer lists, and, invariably, I am not getting the concatenation on the last element. Any ideas?
CodePudding user response:
That's the expected behaviour - you're concating strings using a separator, no reason for the separator to be thrown at the end. Just add it manually.
asTextWithNS = asSeries.str.cat(sep='.NS ')
print(asTextWithNS '.NS')
Outputs
HCLTECH.NS ADANIPORTS.NS HDFC.NS TECHM.NS ONGC.NS DIVISLAB.NS TCS.NS
