I need to extract names/strings from a .txt file line by line. I am trying to use regex to do this. Eg. In this below line I want to extract the name “Victor Lau”, “Siti Zuan” and the string “TELEGRAPHIC TRANSFER” in three different lists then output them into an excel file…
Tag: python-3.x
Pandas Dataframe into nested Dictionaries conversion in Python
I need a little help. I want to convert from dataframe into nested dictionaries. and i want to convert in this format: Answer We can do groupby with agg dict items
Extract duplicity without rearranging the column and find cumsum in python
I have a dataset with 4000 rows, where I have the duplicate rows(e.g. 2, 3, 4 times). I want to find the cumsum of the duplicates over time. I have used this code to assign the number of duplicity. But it has rearranged the position of ID Output whereas I want to add the duplicity and the ID remains same
cv2.imwrite doesn’t save result image but initial only
I have a code: that generates grid like this one: So cv2.imshow shows the grid but cv2.imwrite writes only initial black background not the grid for some reason. How to fix that? Answer You need to scale the color channels: cv2.imwrite(“background.jpg”, background) Alternatively you can create a &…
How can I fix this key error when trying to import a shell variable into python?
This question has been asked before here, but when I try to emulate the correct answer I get a key error, and I’m wondering what I’m doing wrong. I have a shell script that creates a directory in home: Then I go over to my python script, and I want to use the variable directory from my shell scrip…
Python large numbers (float and integer)
I am trying to understand the following calculation results. Out[1], Out[2] and Out[3] seem to be related to the limit on precision of floats, and Out[4] may be due to the fact that there is no limit on digits of int. Correct? I wonder if someone can explain them in more detail. Answer To understand why this …
Comparing data between two CSV files to move data to a third CSV file
I have this csv file, file1.csv: Then file2.csv: For every line in file2.csv that has ‘CORP’, ‘STORE’, or ‘NEWS’, I already searched through file1.csv and created a file, such as STOREall.csv, CORPall.csv, and NEWSall.csv. I want OUs, such as ICECREAM, SECURITY, and DELI to…
Select only available rows of a pandas dataframe
Let say I have the following pandas df Now I have another array Clearly, the element ‘x’ is not available in my original df. How can I select rows of df based on select but choose only available rows without any error? i.e. in this case, I want to select only rows corresponding to ‘c’ …
How to update a value of a cell of a dataframe using the value of two cells below?
I have the following dataframe (created for an example): Assuming that this is a long dataframe, whenever there is a value of 2 in row B, I need to replace that value with the value that is two cells below that. I tried: But I get: And I need: Does anyone have suggestions for leaving rows that do not equal
How to convert Linux date string to Python Datetime
I am trying to convert the Linux date time ‘Fri Feb 25 16:07:17 UTC 2022’ to python datetime but not able to achieve the same. However, I am able to do using below code, but still looking for the right approach: Input -> Fri Feb 25 16:07:17 UTC 2022 Output -> ‘2022-02-25 16:07:17’…