Issue
I have a dictionary linking coordinates to a column name from a dataframe
label_xpos = dict(zip(sensor_label, sensor_x_coordinates))
I want to detect in my df, the empty column, and for each instance, create a list of valid coordinates
for i in range (0,10):
nan_columns = df.columns[df.iloc[i].isna()].tolist()
print(nan_columns)
xclean = ??
It is silly really because I can do it the other way around (finding the points that are not correct)
xclean = [label_xpos[column] for column in nan_columns]
Thank you
Solution
It is silly really because I can do it the other way around..
If I understand you correctly, you need notna
that is the opposite of isna
:
for i in range (0,10):
notna_columns = df.columns[df.iloc[i].notna()].tolist()
xclean = [label_xpos[column] for column in notna_columns]
Note that you can avoid processing each row separately and use @
dot product :
from operator import itemgetter
notna_cols = (df.notna() @ (df.columns + ",")).str[:-1].str.split(",").to_dict()
# {0: ['A', 'B', 'C'], 1: ['A', 'C'], 2: ['B', 'C']}
for index, cols in notna_cols.items():
print(f"{index=}:", itemgetter(*cols)(label_xpos))
# index=0: (11, 22, 33)
# index=1: (11, 33)
# index=2: (22, 33)
Used input :
import pandas as pd
sensor_label = list("ABC")
sensor_x_coordinates = [11, 22, 33]
label_xpos = dict(zip(sensor_label, sensor_x_coordinates))
df = pd.DataFrame([[1, 2, 3], [4, None, 6], [None, 8, 9]], columns=sensor_label)
# A B C
# 0 1.00 2.00 3
# 1 4.00 NaN 6
# 2 NaN 8.00 9
[3 rows x 3 columns]
Answered By - Timeless
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.