![](/img/trans.png)
[英]Can someone consolidate the definition and the differences between a list, an array, a numpy array, a pandas dataframe , series?
[英]Pandas: Creating a list based on the differences between 2 series
当 2 个 Pandas 系列不相等并且想使用“<”来指出差异时,我正在编写一条自定义错误消息。
这是失败的平等的工作流程:
pd.Series([list])
table = pd.concat([list1], [list2]), axis=1
table.columns = ['...', '...']
, table.index = ['...', '...']
当前output:
|你的|实际的|
|1|1|
|2|2|
|4|3|
所需的 output:
|你的|实际|-|
|1|1||
|2|2||
|4|3|<|
天真的解决方案是遍历每个列表索引,如果它不相等,则将“<”附加到另一个列表,然后将此列表放入pd.concat()
但我正在寻找一种使用 Pandas 的方法。例如,
error_series = '<' if (abs(yours - actual) >= 1).all(axis=None) else ''
理想情况下,如果结果之间的差异大于 1 的误差幅度,则 append '<' 到列表,否则 append 没有
注意:由于 StackOverflow 很挑剔并且不让我发布我的问题而删除了表格
您可以创建 DF 并在一行中给出索引和列名:
import pandas as pd
list1 = [1,2,4]
list2 = [1,2,10]
df = pd.DataFrame(zip(list1, list2), columns=['Yours', 'Actual'])
创建一个 boolean 掩码以查找差异过大的行:
margin_of_error = 1
mask = df.diff(axis=1)['Actual'].abs()>margin_of_error
向 DF 添加一列并根据需要设置掩码的值:
df['too_different'] = df.diff(axis=1)['Actual'].abs()>margin_of_error
df['too_different'].replace(True, '<', inplace=True)
df['too_different'].replace(False, '', inplace=True)
output:
Yours Actual too_different
0 1 1
1 2 2
2 4 10 <
或者你可以这样做:
df = df.assign(diffr=df.apply(lambda x: '<'
if (abs(x['yours'] - x['actual']) >= 1)
else '', axis=1))
print(df)
'''
yours actual diffr
0 1 1
1 2 2
2 4 3 <
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.