Extracting specific text strings from a cell - excel-formula

Please take a look at the below text:
Mr. Manimaran R MMTFD NL20824448862,Room no-206
I have around half a million cells like this and i want to extract the ID number from this text using a formula. The problem that i am facing is that some ids start with NL some with NH and some with IN/IWA and the length of the text string containing the ids is also different (13/14/15 etc).
Is there any formula through which i can extract these ids??
Currently I am using this formula =MID(text,FIND("NH",text,1),15).
But i need to change it everytime for NL/IN as also for 14/15 etc.

Use of external tools is the way to go with that much variance in what you need to extract.

How about the following (assuming text is in cell C3 (basically pivoting off the comma and looking back)
=right(mid(C3, find(",",C3)-16,16),len(mid(C3, find(",",C3)-16,16))-find(" ",mid(C3, find(",",C3)-16,16)))

You can use pandas with python3 to easily and quickly do such task.
In python3 shell
import pandas as pd
df= pd.read_excel('./your_excel_file.xlsx') ##import from excel
df['ID']=df['Data_header_name'].str.split(' ',expand=True)[4].str.split(',',expand=True)[0] ##here you can use any relevant filters to get what you want
df.to_excel('output_excel.xlsx') ## save to excel
You also need to install openpyxl, xlrd, numpy, pandas using pip
pip install openpyxl xlrd numpy pandas

Related

Countif function in Python looping in every cell

enter image description here
Hey everyone I am used to work in excel but recently after getting a dataset of about 500k rows that need to be worked in the same worksheet I have huge capacity issues and I was advised to try and transition any function to a python environment. So this excel function "=IF(COUNTIF($J$2:J3,J3)>1,0,1)"~J is the column of the Asset ID~ goes to each cell and if it has previous encountered it in the cells above it returns 0 and if it is unique it returns 1.
How that would be possible in a python environment if I load my table as a DataFrame?
Thanks in advance
You can use pandas to achieve this very easily:
import pandas as pd # import pandas
df = pd.read_excel('your_file.xlsx') # use appropriate function according to your file type
df['Unique'] = ~df[Asset_Id].duplicated().astype(int) # places 1 where it is not encountered before, 0 elsewhere

Display full Pandas dataframe in Jupyter without index

I have a pandas dataframe that I would like to pretty-print in full (it's ~90 rows) in a Jupyter notebook. I'd also like to display it without the index column, if possible. How can I do that?
For pretty-printing without an index, I think the right approach is to call the display method for HTML (which is what jupyter does under the hood):
from IPython.display import HTML
HTML(df.to_html(index=False))
(Credit to Display pandas dataframe without index)
As others have suggested you can use pd.display_max_rows() for the row count limitation.
In pandas you can use this
pd.set_option("display.max_rows", None, "display.max_columns", None)
please use this.
Without index use additionally.
df.to_string(index=False)

Python Read Excel Value with Format Currency

I have Excel file(.xls) with Value and Currency (Example: USD, MYR... using format cell currency)
when i use xlrd to read, it only contain value only.
i search through online, openpyxl have this function, but not supported .xls file.
i need to know which currency is using.
may i know any other method?
Try reading using pandas
import pandas as pd
df = pd.read_excel('Your xls filename')
And now you can access your columns like this
print(df[col1])
For more info, refer - https://pythonspot.com/read-xls-with-pandas/

how to search a text file in python 3

I have this text file that has lists in it. How would I search for that individual list? I have tried using loops to find it, but every time it gives me an error since I don't know what to search for.
I tried using a if statement to find it but it returns -1.
thanks for the help
I was doing research on this last night. You can use pandas for this. See here: Load data from txt with pandas. One of the answers talks about list in text files.
You can use:
data = pd.read_csv('output_list.txt', sep=" ", header=None)
data.columns = ["Name", "b", "c", "etc."]
Add sep=" " in your code, leaving a blank space between the quotes. So pandas can detect spaces between values and sort in columns. Data columns isenter code here for naming your columns.
With a JSON or XML format, text files become more searchable. In my research I’ve decided to go with an XML approach. Here is the link to a blog that explains how do use Python with XML: http://www.austintaylor.io/lxml/python/pandas/xml/dataframe/2016/07/08/convert-xml-to-pandas-dataframe.
If you want to search the data frame try:
import pandas as pd
txt_file = 'C:\path\to\your\txtfile.txt'
df = pd.read_table(txt_file, sep = ",")
row = df.loc[df['Name'] == 'bob']
Print(row)
Now depending how your text file is formated, your results will not work for every text file. The idea of a dataframe in pandas helps u create a CSV file formats. This giving the process a repeatable structure to enable testing results. Again I recommend using a JSON or XML format before implementing pandas data frames in ur solution. U can then create a consistent result, that is testable too!

I need to change the value of a specific column of a dataframe using condition format while imported multiple Excel file

import pandas as pd
batch=pd.read_excel('batch.xlsx')
stock_report=pd.read_excel('Stock_Report.xlsx')
Result_stock=pd.merge(stock_report,batch[['Batch','Cost price']], on='Batch').fillna(0)
Result_stock2=pd.merge(Result_stock,batch[['Item number',' Batch MRP']], on='Item number').fillna(0)
Result_stock2['Total']=Result_stock2['Posted quantity']*Result_stock2['Cost price']
I need to change the value of Column(Total) for Result_stock2 by multiplying it with two column value if it has 0.
You need to learn some formatting. Please format your code so we can read.
If I understood what you mean and your script is working fine so far, you should just simply add:
Result_stock2.loc[Result_stock2['Total']==0,'Total']=(****OPERATION YOU NEED****)
example in 'OPERATION'
Result_stock2.loc[Result_stock2['Total']==0,'Posted quantity']*(Result_stock2.loc[Result_stock2['Total']==0,'Cost price']-5)
It's not a beautiful code but will do what you need.

Resources