Pandas extracts text from cells and slices it up
For example, there is the following Excel data
Now we want to extract the contents of one of the payment time columns and capture only the specific time on the day of shipment.
Use . () to solve this problem.
The code is as follows:
import pandas as pd file = pd.read_excel(r"C:\Users\15025\Desktop\uncle\") time = file["Time of payment"].(11, 19) print(time) """ result. 0 23:57:14 1 23:47:16 2 23:47:15 3 23:43:14 4 23:40:30 5 23:28:40 6 23:28:44 7 23:25:42 8 23:23:57 9 23:22:11 10 23:21:47 Name: payment time, dtype: object """
As you can see, we have successfully intercepted the time information in it.
The above methods are morepandas
The write-up.
There is also another alternative
The code is as follows:
import pandas as pd file = pd.read_excel(r"C:\Users\15025\Desktop\uncle\") for i in range(len(file["Time of payment"])): print(file["Time of payment"][i][11:19]) """ result: 23:57:14 23:47:16 23:47:15 23:43:14 23:40:30 23:28:40 23:28:44 23:25:42 23:23:57 23:22:11 23:21:47 """
As you can see, we also managed to get the information we wanted, but we used a loop.
While the speed doesn't feel too slow, a lot of data testing is still needed to show the advantages and disadvantages of both methods.
The first one is currently recommendedpandas
The built-in methods of the
What if you want to work on the last row of data?
The following code can be used:
import pandas as pd file = pd.read_excel(r"C:\Users\15025\Desktop\uncle\") time = [-1].tolist() time1 = [-1] print(time) print(time1) """ ['SXDD202112212321341427301514', '2021-12-21 23:21:34', '2021-12-21 23:21:47', '2021122122001498451403927933', 'G202112212321334217301915'] ['SXDD202112212321341427301514' '2021-12-21 23:21:34' '2021-12-21 23:21:47' '2021122122001498451403927933' 'G202112212321334217301915'] """
You can see that we have successfully converted the last row of data into a list object
Next, you can use list slicing as normal to get the data we need.
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.