Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

pandas - Python: selecting columns for plotting based on values on rows

I have a data frame that looks like

Angle 1 2 3 4
Wavelength 20.5 677.8 445.76 345.76 987.5
1 56 432.56 123.65 545.76 456.65
2 67 9568.7 456.53 564.987 5675.4
3 62 9568.7 456.53 564.987 5675.4
5 72 9568.7 456.53 564.987 5675.4
question from:https://stackoverflow.com/questions/66061785/python-selecting-columns-for-plotting-based-on-values-on-rows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
df.columns
# Index(['index', 'Angle', '1', '2', '3', '4'], dtype='object')


# step1. find row `Wavelength`
cond = df['index'] == 'Wavelength'
row = df[cond].iloc[0, 2:]
# row = df.loc[0, '1':]  # or use iloc -> df.iloc[0, 2:]

# step2. find which colums is closest to 500
cond = abs(row - 500) == abs(row - 500).min()
col = list(row[cond].index)

print(col) # ['2']

# as index is x-axis, col is y-axis
df.set_index('Angle')[col].plot()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...