从列表输出中删除“ dtype”和“ object”

时间:2020-06-21 17:51:09

标签: python pandas

我编写了一个函数,该函数应该在特定条件下基于数据帧返回一些值。

def myFun(declaration, policynumber, ID):
    tempvar = df.loc[(df['Moneyamount'] == 70) & (df['ID'] == ID) & (df['Nr deklaracji'] == declaration) & (df['Nr Polisy'] == policynumber)]
    if len(tempvar) == 1:
        info = [declaration, 'YES', ID, tempvar['Birthdate'], tempvar['City'], tempvar['Street'], tempvar['Housenumber']]
    elif len(tempvar) == 0:
        info = [declaration, 'NO', ID, tempvar['Birthdate'], tempvar['City'], tempvar['Street'], tempvar['Housenumber']]
    return(info)

一切正常,但结果如下:

[90052,
 'YES',
 58041991951,
 0    19-04-58
 Name: Birthdate, dtype: object,
 0    Warsaw
 Name: City, dtype: object,
 0    Bluestreet
 Name: Street, dtype: object,
 0    6
 Name: Housenumber, dtype: object]

但是,我的任务不仅是使该功能正常工作,而且还要将结果打印到.json。但是问题是,当我尝试将其打印到.json文件中时,它保留了“ 0,Name :, dtype:object”部分,而我想摆脱它。

我通常浏览过stack和google,但是通常的解决方法是在整个列表具有相同数据类型的情况下。

谢谢。

编辑: 我找到了一种解决方法:

我添加了

.tolist()[0]

每个引用数据框的值之后,就像这样

    info = [declaration, 'NO', ID, tempvar['Birthdate'].tolist()[0], tempvar['City'].tolist()[0], tempvar['Street'].tolist()[0], tempvar['Housenumber'].tolist()[0]]

这使我可以接收整齐的.json格式。这不是最好的解决方案,但在这里对我有用。感谢您的回答。

1 个答案:

答案 0 :(得分:1)

如果您直接从过滤后的数据中获取所需的列,例如:

Traceback (most recent call last): File "../src/Main.py", line 11, in <module> controller.train() File "/home/myuser/gbt/src/controller/ModelController.py", line 62, in train gbt_model.train_gbt_model() File "/home/myuser/gbt/src/ml_model/TensorFlowModel.py", line 78, in train_gbt_model self._train_with_gbt_classifier_v2(train_input_fn, eval_input_fn, save_model=False) File "/home/myuser/gbt/src/ml_model/TensorFlowModel.py", line 292, in _train_with_gbt_classifier_v2 gbt_est.train(train_input_fn) File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 349, in train loss = self._train_model(input_fn, hooks, saving_listeners) File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1182, in _train_model return self._train_model_default(input_fn, hooks, saving_listeners) File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1213, in _train_model_default return self._train_with_estimator_spec(estimator_spec, worker_hooks, File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1502, in _train_with_estimator_spec with training.MonitoredTrainingSession( File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/training/monitored_session.py", line 601, in MonitoredTrainingSession return MonitoredSession( File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/training/monitored_session.py", line 1034, in __init__ super(MonitoredSession, self).__init__( File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/training/monitored_session.py", line 737, in __init__ h.begin() File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/training/basic_session_run_hooks.py", line 563, in begin self._summary_writer = SummaryWriterCache.get(self._checkpoint_dir) File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/summary/writer/writer_cache.py", line 62, in get FileWriterCache._cache[logdir] = FileWriter( File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/summary/writer/writer.py", line 372, in __init__ super(FileWriter, self).__init__(event_writer, graph, graph_def) File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/summary/writer/writer.py", line 84, in __init__ self.add_graph(graph=graph, graph_def=graph_def) File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/summary/writer/writer.py", line 194, in add_graph true_graph_def = graph.as_graph_def(add_shapes=True) File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3136, in as_graph_def result, _ = self._as_graph_def(from_version, add_shapes) File "/home/myuser/gbt/venv/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3053, in _as_graph_def graph.ParseFromString(compat.as_bytes(data)) google.protobuf.message.DecodeError: Error parsing message

尝试:

df[df['column_name']==value][column_name]