[英]Pandas - Append rows to DataFrames as elements in a list, not working
[英]Conditional append list for Pandas dataframes
我正在使用以下代码尝试过滤我们的特定行,但我收到一条错误消息:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
df = pd.DataFrame(rdataset)
plane = 'axial'
contrast = 'T1'
a = []
for slices in range(0,60):
if df['nSlices']==slices: #---------------> Problem here ?
path = df.loc[df['nSlices'].eq(slices) & df['Orient'].eq(plane) & df['Contrast'].eq(contrast),'Path'].tolist()
a.append(path)
出现错误是因为您试图将 Series 对象df['nSlices']
与标量整数slices
。 这种比较的结果是带有 bool 值的 Series 对象(尝试执行print(df['nSlices']==slices)
来查看它)并且您不能在 if 语句中使用它。
要解决该问题,只需删除if df['nSlices']==slices:
以获得:
for slices in range(0,60):
path = df.loc[df['nSlices'].eq(slices) & df['Orient'].eq(plane) & df['Contrast'].eq(contrast),'Path'].tolist()
a.append(path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.