考虑以下熊猫数据框:
df = pd.DataFrame({'t': [1,2,3], 'x1': [4,5,6], 'x2': [7,8,9]} )
>>> print(df)
t x1 x2
0 1 4 7
1 2 5 8
2 3 6 9
Run Code Online (Sandbox Code Playgroud)
我想对名称包含字符“x”的列应用一个函数(比如乘以 2)
这可以通过以下方式完成:
df.filter(regex='x').apply(lambda c: 2*c)
Run Code Online (Sandbox Code Playgroud)
但没有到位。我的解决办法是:
tmp = df.filter(regex='x')
tmp = tmp.apply(lambda c: 2*c)
tmp['t'] = df['t']
df = tmp
Run Code Online (Sandbox Code Playgroud)
这增加了更改列顺序的问题。有没有更好的办法?
我知道人们可以将绘图默认颜色序列视为:
import plotly.express as px
print(px.colors.qualitative.Plotly)
Run Code Online (Sandbox Code Playgroud)
生成 CSS 颜色列表:
['#636EFA', '#EF553B', '#00CC96', '#AB63FA', '#FFA15A', '#19D3F3', '#FF6692', '#B6E880', '#FF97FF', '#FECB52']
Run Code Online (Sandbox Code Playgroud)
我可以通过从此列表中选择一个元素来指定绘图的颜色。例如:
go.Scatter(x=x, y=y, line={"color": '#636EFA'})
Run Code Online (Sandbox Code Playgroud)
在plotly中是否为这些颜色定义了更多用户友好的名称,或者CSS颜色是引用它们的唯一方法?