我有一个像这样的pandas DataFrame:
molecule species
0 a [dog]
1 b [horse, pig]
2 c [cat, dog]
3 d [cat, horse, pig]
4 e [chicken, pig]
Run Code Online (Sandbox Code Playgroud)
我喜欢提取一个只包含行的DataFrame,其中包含任何行selection = ['cat', 'dog'].所以结果应该是这样的:
molecule species
0 a [dog]
1 c [cat, dog]
2 d [cat, horse, pig]
Run Code Online (Sandbox Code Playgroud)
最简单的方法是什么?
用于检测:
selection = ['cat', 'dog']
df = pd.DataFrame({'molecule': ['a','b','c','d','e'], 'species' : [['dog'], ['horse','pig'],['cat', 'dog'], ['cat','horse','pig'], ['chicken','pig']]})
Run Code Online (Sandbox Code Playgroud) 我有一个带有浮点(十进制)索引的pandas DataFrame,我用它来查找值(类似于字典).由于浮点数不完全是它们应该将所有值乘以10,并.astype(int)在将其设置为索引之前将其转换为整数.然而,这似乎做了一个floor而不是四舍五入.因此1.999999999999999992被转换为1而不是2.使用pandas.DataFrame.round()之前的方法舍入不会避免此问题,因为值仍然存储为浮点数.
最初的想法(显然是一个关键错误)是这样的:
idx = np.arange(1,3,0.001)
s = pd.Series(range(2000))
s.index=idx
print(s[2.022])
Run Code Online (Sandbox Code Playgroud)
尝试转换为整数:
idx_int = idx*1000
idx_int = idx_int.astype(int)
s.index = idx_int
for i in range(1000,3000):
print(s[i])
Run Code Online (Sandbox Code Playgroud)
输出总是有点随机,因为整数的"实数"值可能略高于或低于所需值.在这种情况下,索引包含值1000的两倍,并且不包含值2999.
我想将 pandas DataFrame 的多列添加到 matplotlib 轴并使用颜色名称列表定义颜色。将列表传递给颜色参数时,出现值错误:无效的 RGBA 参数。以下 MWE 重现了此错误:
import pandas as pd
from matplotlib import pyplot as plt
import matplotlib.patches as mpatches
df = pd.DataFrame({'0':[0,1,0],'a':[1,2,3],'b':[2,4,6],'c':[5,3,1]})
colors = ['r','g','b']
fig, ax = plt.subplots()
ax.bar(df.index.values,df['0'].values, color = 'y')
ax2 = ax.twinx()
h = ax2.plot(df.index.values, df[['a','b','c']].values, color = colors)
handles = [mpatches.Patch(color='y')]
handles = handles + h
labels = df.columns()
lgd=ax.legend(handles,labels,loc='center left', bbox_to_anchor=(1.1, 0.5), ncol=1, fancybox=True, shadow=True, fontsize=ls)
plt.savefig('test.png', bbox_extra_artists=(lgd,tbx), bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud) 我正在尝试在彼此上方绘制两个 imshow 和一个图,共享它们的 x 轴。图形布局是使用 gridspec 设置的。这是一个 MWE:
import matplotlib as mpl
from matplotlib import pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10,8))
gs = fig.add_gridspec(3,2,width_ratios=(1,2),height_ratios=(1,2,2), left=0.1,right=0.9,bottom=0.1,top=0.99, wspace=0.1, hspace=0.1)
ax=fig.add_subplot(gs[2,1])
ax2=fig.add_subplot(gs[2,0], sharey=ax)
ax3=fig.add_subplot(gs[1,0])
ax4=fig.add_subplot(gs[1,1], sharex=ax, sharey=ax3)
ax5=fig.add_subplot(gs[0,1], sharex=ax)
dates = pd.date_range("2020-01-01","2020-01-10 23:00", freq="H")
xs = mpl.dates.date2num(dates)
ys = np.random.random(xs.size)
N = 10
arr = np.random.random((N, N))
arr2 = np.random.random((N, N))
norm=mpl.colors.Normalize(0, arr.max()) # change the min to stretch the color spectrum
pcm = ax.imshow(arr, extent=[xs[0],xs[-1],10,0],norm=norm,aspect='auto')
cax = fig.colorbar(pcm, …Run Code Online (Sandbox Code Playgroud) 我在散点图中绘制数据,我想看到滞后的方向。有没有人知道如何在每条线上实现指向下一点方向的箭头?
或者,标记可以由指向下一个点的方向的箭头代替。
我在找什么:
获取绘图的代码(无箭头):
df = pd.DataFrame.from_dict({'x' : [0,3,8,7,5,3,2,1],
'y' : [0,1,3,5,9,8,7,5]})
x = df['x']
y = df['y']
fig, ax = plt.subplots()
ax.scatter(x,y)
ax.plot(x,y)
Run Code Online (Sandbox Code Playgroud) python ×5
matplotlib ×3
pandas ×3
colors ×1
dataframe ×1
imshow ×1
int ×1
rounding ×1
scatter-plot ×1