Pandas 如何将数据帧的行全部绘制为线?
假设我们有以下数据帧:Pandas 如何将数据帧的行全部绘制为线?,pandas,matplotlib,Pandas,Matplotlib,假设我们有以下数据帧: import pandas as pd df = pd.DataFrame( [ ['Norway' , 'beta', 30.0 , 31.0, 32.0, 32.4, 32.5, 32.1], ['Denmark' , 'beta', 75.7 , 49.1, 51.0, 52.3, 50.0, 47.9], ['Switzerland', 'beta', 4
import pandas as pd
df = pd.DataFrame(
[
['Norway' , 'beta', 30.0 , 31.0, 32.0, 32.4, 32.5, 32.1],
['Denmark' , 'beta', 75.7 , 49.1, 51.0, 52.3, 50.0, 47.9],
['Switzerland', 'beta', 46.9 , 44.0, 43.5, 42.3, 41.8, 43.4],
['Finland' , 'beta', 29.00, 29.8, 27.0, 26.0, 25.3, 24.8],
['Netherlands', 'beta', 30.2 , 30.1, 28.5, 28.2, 28.0, 28.0],
],
columns = [
'country',
'run_type',
'score A',
'score B',
'score C',
'score D',
'score E',
'score F'
]
)
df
如何将分数值绘制为线条,其中每条线条对应一个国家?尝试绘制数据框的转置:
# the score columns, modify if needed
score_cols = df.columns[df.columns.str.contains('score')]
df.set_index('country')[score_cols].T.plot()
输出:
尝试绘制数据帧的转置:
# the score columns, modify if needed
score_cols = df.columns[df.columns.str.contains('score')]
df.set_index('country')[score_cols].T.plot()
输出:
既然您标记了matplotlib,下面是一个使用plt.plot的解决方案。其思想是使用iloc按行绘制线条
既然您标记了matplotlib,下面是一个使用plt.plot的解决方案。其思想是使用iloc按行绘制线条
该死,如果我能选择两个正确的答案,我会的!谢谢你干净的建议。该死,如果我能选择两个正确的答案,我会的!谢谢你干净的建议。