Python 如果数据帧中的字符串等于字典的键,如何将字典值插入到数据帧中?
我是python新手,希望您能给予我帮助。我正在创建一个脚本,为工程问题生成荷载工况组合。当字典的键为==到数据框中的字符串时,是否有一种简洁的方法用一个列表字典值替换像下面的input_eq这样的数据框的值Python 如果数据帧中的字符串等于字典的键,如何将字典值插入到数据帧中?,python,pandas,dataframe,Python,Pandas,Dataframe,我是python新手,希望您能给予我帮助。我正在创建一个脚本,为工程问题生成荷载工况组合。当字典的键为==到数据框中的字符串时,是否有一种简洁的方法用一个列表字典值替换像下面的input_eq这样的数据框的值 input_eq= 1st_coeff 1st_load 2nd_coeff 2nd_load 3rd_coeff 3rd_load 4th_coeff 4th_load 5th_coeff ... 6th_load 7th_coeff
input_eq=
1st_coeff 1st_load 2nd_coeff 2nd_load 3rd_coeff 3rd_load 4th_coeff 4th_load 5th_coeff ... 6th_load 7th_coeff 7th_load 8th_coeff 8th_load 9th_coeff 9th_load 10th_coeff 10th_load
Load case ...
SLSc4-1 1.00 G 1.00 GH 1.000 G0I 0.5 GT 0.50 ... QLbis 0.3 QTbis 0.6 Qwbis 1.0 QWLEFbis 0.2 Dyna_m
ULSf1b-1 1.35 G 1.35 G0Ibis 0.675 GT 1.5 QC 0.45 ... Qwbis 1.2 QWLEFbis NaN NaN NaN NaN NaN NaN
load_variant_dict={'G': ['G'], 'G0I': ['G0I'], 'G0Ibis': ['G0Ibis', '0'], 'GH': ['GH'], 'GT': ['GT'], 'QC': ['QC'], 'QLbis': ['LC0', 'QL'], 'QTbis': ['LC0', 'Qtsummer', 'Qtwinter'], 'Qwbis': ['LC0', 'QwN', 'QwW', 'QwE', 'QwS'], 'QWLEFbis': ['LC0', 'QWLEF'], 'Dyna_m': ['Dyna_m_1', 'Dyna_m_2', 'Dyna_m_3', 'Dyna_m_4', 'Dyna_m_5', 'Dyna_m_6', 'Dyna_m_7', 'Dyna_m_8', 'Dyna_m_9', 'Dyna_m_10', 'Dyna_m_11', 'Dyna_m_12', 'Dyna_m_13', 'Dyna_m_14', 'Dyna_m_15', 'Dyna_m_16', 'Dyna_m_17', 'Dyna_m_18', 'Dyna_m_19', 'Dyna_m_20', 'Dyna_m_21', 'Dyna_m_22', 'Dyna_m_23', 'Dyna_m_24'], 'Aexpbis': ['AexpN', 'AexpW', 'AexpE', 'AexpS'], 'AWL': ['AWL'], 'AT': ['AT'], 'ATp': ['ATdp', 'Atsp'], 'ATw': ['ATwN', 'ATwW', 'ATwE', 'ATwS']}
理想情况下,对于输出,我希望单元格中有一个列表,其中有多个变量。这样我就可以在数据帧上使用笛卡尔积来生成所有可能的负载组合。
我尝试过使用.replace()方法,但是一旦有超过1个变量,例如在第二个加载列中,脚本将返回以下错误
>>> input_eq_b= input_eq_a.replace({'2nd_load':input_var_dict})
ValueError: cannot assign mismatch length to masked array
>>> print(input_eq_b)
我认为这是因为函数不能用字符串列表替换字符串,所以任何关于替代项的帮助都将非常有用
Desired output example:
Load case 1st_coeff 1st_load 2nd_coeff 2nd_load 3rd_coeff 3rd_load 4th_coeff 4th_load 5th_coeff 5th_load 6th_coeff 6th_load 7th_coeff 7th_load 8th_coeff 8th_load \
ULSf1b-1 1.35 G 1.35 [G0Ibis,0] 0.675 GT 1.5 QC 0.45 [LCO,Qtsummer,Qtwinter] 0.9 [LC0, QwN, QwW, QwE, QwS] 1.2 [LCO,QWLEF] NaN NaN
您可以使用以下内容:
df = pd.DataFrame({'col1' : ['a','b','c']})
print('before:\n', df)
md = {'a' : ['a1','a2','a3'], 'b' : ['b1','b2']}
df['col1'] = [md.get(key,key) for key in df['col1']]
print('after:\n',df)
这将生成此玩具示例所需的输出
before:
col1
0 a
1 b
2 c
after:
col1
0 [a1, a2, a3]
1 [b1, b2]
2 c
注意dict.get(a,b)
方法,如果a是现有密钥,则返回dict[a],如果a不在密钥中,则返回b
将列表作为数据帧中的单元格是可能的(如上面的示例所示),但通常认为这不是一个好主意,请参见一些讨论