![](/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.