2

我不知道为什么打印时id_1 & id_2的原始内容会发生变化。

我有一个名为test_data.json的 json 文件

{
"objects":{
    "value":{
        "1298543947669573634":{
            "timestamp":"Wed Aug 26 08:52:57 +0000 2020",
            "id_1":"1298543947669573634",
            "id_2":"1298519559306190850"
            }
        }
    }
}

输出

python test_data.py 
                  id_1                 id_2                 timestamp
0  1298543947669573632  1298519559306190848 2020-08-26 08:52:57+00:00

我的名为test_data.py的代码是

import pandas as pd
import json

file = "test_data.json"
with open (file, "r")  as f:
    all_data = json.loads(f.read()) 
data = pd.read_json(json.dumps(all_data['objects']['value']), orient='index')
data = data.reset_index(drop=True)
print(data.head())

我该如何解决这个问题,以便正确解释数值?

4

2 回答 2

1
  • 使用python 3.8.5pandas 1.1.1

当前实施

  • 首先,代码读取文件并将其从 astr类型转换为 a dictjson.loads
with open (file, "r")  as f:
    all_data = json.loads(f.read()) 
  • 然后'value'转换回一个str
json.dumps(all_data['objects']['value'])
pd.read_json(json.dumps(all_data['objects']['value']), orient='index')

更新代码

选项1

  • 使用pandas.DataFrame.from_dict然后转换为数字。
file = "test_data.json"
with open (file, "r")  as f:
    all_data = json.loads(f.read()) 

# use .from_dict
data = pd.DataFrame.from_dict(all_data['objects']['value'], orient='index')

# convert columns to numeric
data[['id_1', 'id_2']] = data[['id_1', 'id_2']].apply(pd.to_numeric, errors='coerce')

data = data.reset_index(drop=True)

# display(data)
                        timestamp                 id_1                 id_2
0  Wed Aug 26 08:52:57 +0000 2020  1298543947669573634  1298519559306190850

print(data.info())
[out]:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 3 columns):
 #   Column     Non-Null Count  Dtype 
---  ------     --------------  ----- 
 0   timestamp  1 non-null      object
 1   id_1       1 non-null      int64 
 2   id_2       1 non-null      int64 
dtypes: int64(2), object(1)
memory usage: 152.0+ bytes

选项 2

  • 使用pandas.json_normalize然后将列转换为数字。
file = "test_data.json"
with open (file, "r")  as f:
    all_data = json.loads(f.read()) 

# read all_data into a dataframe
df = pd.json_normalize(all_data['objects']['value'])

# rename the columns
df.columns = [x.split('.')[1] for x in df.columns]

# convert to numeric
df[['id_1', 'id_2']] = df[['id_1', 'id_2']].apply(pd.to_numeric, errors='coerce')

# display(df)
                        timestamp                 id_1                 id_2
0  Wed Aug 26 08:52:57 +0000 2020  1298543947669573634  1298519559306190850

print(df.info()
[out]:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Data columns (total 3 columns):
 #   Column     Non-Null Count  Dtype 
---  ------     --------------  ----- 
 0   timestamp  1 non-null      object
 1   id_1       1 non-null      int64 
 2   id_2       1 non-null      int64 
dtypes: int64(2), object(1)
memory usage: 152.0+ bytes
于 2020-09-06T01:25:46.987 回答
0

这是由问题 20608引起的,并且仍然发生在当前 1.2.4 版本的 Pandas 中。

这是我的解决方法,它在我的数据上甚至比read_json

def broken_load_json(path):
    """There's an open issue: https://github.com/pandas-dev/pandas/issues/20608
    about read_csv loading large integers incorrectly because it's converting
    from string to float to int, losing precision."""
    df = pd.read_json(pathlib.Path(path), orient='index')
    return df

def orjson_load_json(path):
    import orjson  # The builting json module would also work
    with open(path) as f:
        d = orjson.loads(f.read())
    df = pd.DataFrame.from_dict(d, orient='index')  # Builds the index from the dict's keys as strings, sadly
    # Fix the dtype of the index
    df = df.reset_index()
    df['index'] = df['index'].astype('int64')
    df = df.set_index('index')
    return df

请注意,我的答案保留了 ID 的值,这在我的用例中是有意义的。

于 2021-05-10T20:03:20.997 回答