从一个系列或字典中添加一个新列到一个数据框,将我的系列索引和一个数据框列映射到键pandas python



我被这个问题困住了,请帮助:

我有一个数据框架和一个系列如下:

import pandas as pd
df1 = pd.DataFrame({"ID": [4,8,35,28,34,34,14,28], 
"cause of failure": [5,3,0,1,7,8,6,7], 
"crash": [0,0,0,1,1,1,1,1]}) 

我有一个级数:

s = pd.DataFrame({'':["None","Design flaw","metal fatigue","Manufacturing flaw","Pilot 
error","Mechanical failure","Improper maintenance","Fire","Corrosion"]})
为了添加新列,我编写了add_column函数,在函数中我将s1转换为#a字典,但是
def add_column(df,s, ref_column, new_column):
dict_s = s.to_dict(orient='dict')
#dict_s = s.loc[0:].to_dict(orient='index')
error', 5: 'Mechanical failure', 6: 'Improper maintenance', 7: 'Fire', 8: 'Corrosion'}
df[new_column] = df[ref_column].map(dict_s)
return df
add_column(df1,s1,"failure code","failure")

#我不想把空头放到字典中,只需要序列

的键值对
{'': {0: 'None', 1: 'Design flaw', 2: 'metal fatigue', 3: 'Manufacturing flaw', 4: 'Pilot error', 5: 'Mechanical failure', 6: 'Improper maintenance', 7: 'Fire', 8: 'Corrosion'}}
#Like this:
{0: 'None', 1: 'Design flaw', 2: 'metal fatigue', 3: 'Manufacturing flaw', 4: 'Pilot error', 5: 'Mechanical failure', 6: 'Improper maintenance', 7: 'Fire', 8: 'Corrosion'}

我找不到这本字典

#然后我要映射这个字典以匹配列"失败代码"中的代码并将其添加为新列"failure"最后,我想在df1数据框架中添加一个新的列失败。

结果如下所示:

ID  failure code    crash   failure
1   8   3   0   Manufacturing flaw
2   35  0   0   None
3   28  1   1   Design flaw
4   34  7   1   Fire
5   34  8   1   Corrosion
6   14  6   1   Improper maintenance
7   28  7   1   Fire

链接到df

您的s不是一个系列,但实际上是一个数据框架。在map中使用Series:

df1['new_column'] = df1['cause of failure'].map(s[''])

输出:

ID  cause of failure  crash            new_column
0   4                 5      0    Mechanical failure
1   8                 3      0    Manufacturing flaw
2  35                 0      0                  None
3  28                 1      1           Design flaw
4  34                 7      1                  Fire
5  34                 8      1             Corrosion
6  14                 6      1  Improper maintenance
7  28                 7      1                  Fire

或直接构建一个系列:

s = pd.Series(["None","Design flaw","metal fatigue","Manufacturing flaw","Pilot error","Mechanical failure","Improper maintenance","Fire","Corrosion"])
df1['new_column'] = df1['cause of failure'].map(s)

相关内容

  • 没有找到相关文章

最新更新