Issue
Here is my Problem, i have this code:
for i, row in df_all.iterrows():
mylist = (row.euclidian_distance)
euclid_data.append(mylist)
mylist1 = (row.day_night)
day_night_data.append(mylist1)
if (len(euclid_data)== 120):
xbar = np.mean(euclid_data)
dn = np.mean(day_night_data)
if(dn <= 0.5 and xbar >= day_thresh):
euclid_track.append(0.0)
elif (dn > 0.5 and xbar >= night_thresh):
euclid_track.append(0.0)
else:
euclid_track.append(xbar)
means.append(np.mean(euclid_data))
if(len(euclid_track) >= 3 and sum(euclid_track[len(euclid_track)-3:]) == 0):
print(row)
warn.append(i)
if(len(warn)>=100):
break
And my result is from print(row):
How do i get the result in pandas dataframe to look like this:
id | date_time | euclidian_distance |
---|---|---|
AA00-0190148 | 2023-10-27 19:23:26 | 82.36223 |
and so forth...Thanks for your help.
Solution
Format field
Create new row from copy with formats
row_copy = row.copy()
# Datetime field
formatted_datetime = row_copy['date_time'].strftime('%Y-%m-%d %H:%M:%S')
row_copy['date_time'] = formatted_datetime
print(row_copy)
Create new dataframe with the filtered data
If you want to save the filtered row, you can create a new dataframe only with those rows. Example:
# [...]
if(len(euclid_track) >= 3 and sum(euclid_track[len(euclid_track)-3:]) == 0):
filtered_rows.append(row)
filtered_df = pd.DataFrame(filtered_rows)
Or create new dataframe from python dictionary or python arrays:
data = {'id': ['AA00-0190148', 'AA00-0190149', ...],
'data_time': [2023-10-27 19:23:26, 2023-10-27 19:23:26, ...],
'euclidian_distance': [82.36223, 91.32223, ...]}
df = pd.DataFrame(data)
Show dataframe in console
If you want to show a table in the console, you can use tabulate, this library shows a table in printable format. You can tabulate a dataframe or a simple Python arrays
Example code:
# Example with dataframe
import pandas as pd
from tabulate import tabulate
dataframe = pd.DataFrame(data, ...)
print(tabulate(dataframe, headers, tablefmt="grid"))
+--------+-------+
| item | qty |
+========+=======+
| spam | 42 |
+--------+-------+
| eggs | 451 |
+--------+-------+
| bacon | 0 |
+--------+-------+
# Example with python arrays
print(tabulate([["Name","Age"],["Alice",24],["Bob",19]], headers="firstrow"))
Name Age
------ -----
Alice 24
Bob 19
Another way is to show the head with print(dataframe.head(n))
(n => number of rows) if you add columns with euclidian_distance.
Answered By - FedeG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.