转换 JSON 数据框 Python 中的数据
Convert JSON data in data frame Python
我是编程语言的初学者,非常感谢您的帮助和支持。
这里是DataFrame,一列的数据是JSON类型?数据。
ID, Name, Information
1234, xxxx, '{'age': 25, 'gender': 'male'}'
2234, yyyy, '{'age': 34, 'gender': 'female'}'
3234, zzzz, '{'age': 55, 'gender': 'male'}'
我想按如下方式隐藏这个 DataFrame。
ID, Name, age, gender
1234, xxxx, 25, male
2234, yyyy, 34, female
3234, zzzz, 55, male
我发现ast.literal_eval()可以将str转为dict类型,但是不知道这个问题的代码怎么写
能否提供一些可以解决此问题的代码示例?
- 如果第三列是 JSON 字符串,
'
无效,应该是 "
,所以我们需要修正这个问题。
- 如果第三列是python
dict
的字符串表示,可以用eval
转换
用于拆分类型为 dict
的第三列并合并到原始 DataFrame
中的代码示例:
data = [
[1234, 'xxxx', "{'age': 25, 'gender': 'male'}"],
[2234, 'yyyy', "{'age': 34, 'gender': 'female'}"],
[3234, 'zzzz', "{'age': 55, 'gender': 'male'}"],
]
df = pd.DataFrame().from_dict(data)
df[2] = df[2].apply(lambda x: json.loads(x.replace("'", '"'))) # fix the data and convert to dict
merged = pd.concat([df[[0, 1]], df[2].apply(pd.Series)], axis=1)
给定 test.csv
ID,Name,Information
1234,xxxx,"{'age': 25, 'gender': 'male'}"
2234,yyyy,"{'age': 34, 'gender': 'female'}"
3234,zzzz,"{'age': 55, 'gender': 'male'}"
- 用
pd.read_csv
and use the converters
parameter with ast.literal_eval
读取文件,这会将 Information
列中的数据从 str
类型转换为 dict
类型。
- 使用
pd.json_normalize
解压 dict
键作为列 headers 和行中的值
.join
具有 df
的规范化列
.drop
Information
列
import pandas as pd
from ast import literal_eval
df = pd.read_csv('test.csv', converters={'Information': literal_eval})
df = df.join(pd.json_normalize(df.Information))
df.drop(columns=['Information'], inplace=True)
# display(df)
ID Name age gender
0 1234 xxxx 25 male
1 2234 yyyy 34 female
2 3234 zzzz 55 male
如果数据不是来自 csv 文件
import pandas as pd
from ast import literal_eval
data = {'ID': [1234, 2234, 3234],
'Name': ['xxxx', 'yyyy', 'zzzz'],
'Information': ["{'age': 25, 'gender': 'male'}", "{'age': 34, 'gender': 'female'}", "{'age': 55, 'gender': 'male'}"]}
df = pd.DataFrame(data)
# apply literal_eval to Information
df.Information = df.Information.apply(literal_eval)
# normalize the Information column and join to df
df = df.join(pd.json_normalize(df.Information))
# drop the Information column
df.drop(columns=['Information'], inplace=True)
我是编程语言的初学者,非常感谢您的帮助和支持。
这里是DataFrame,一列的数据是JSON类型?数据。
ID, Name, Information
1234, xxxx, '{'age': 25, 'gender': 'male'}'
2234, yyyy, '{'age': 34, 'gender': 'female'}'
3234, zzzz, '{'age': 55, 'gender': 'male'}'
我想按如下方式隐藏这个 DataFrame。
ID, Name, age, gender
1234, xxxx, 25, male
2234, yyyy, 34, female
3234, zzzz, 55, male
我发现ast.literal_eval()可以将str转为dict类型,但是不知道这个问题的代码怎么写
能否提供一些可以解决此问题的代码示例?
- 如果第三列是 JSON 字符串,
'
无效,应该是"
,所以我们需要修正这个问题。 - 如果第三列是python
dict
的字符串表示,可以用eval
转换
用于拆分类型为 dict
的第三列并合并到原始 DataFrame
中的代码示例:
data = [
[1234, 'xxxx', "{'age': 25, 'gender': 'male'}"],
[2234, 'yyyy', "{'age': 34, 'gender': 'female'}"],
[3234, 'zzzz', "{'age': 55, 'gender': 'male'}"],
]
df = pd.DataFrame().from_dict(data)
df[2] = df[2].apply(lambda x: json.loads(x.replace("'", '"'))) # fix the data and convert to dict
merged = pd.concat([df[[0, 1]], df[2].apply(pd.Series)], axis=1)
给定 test.csv
ID,Name,Information
1234,xxxx,"{'age': 25, 'gender': 'male'}"
2234,yyyy,"{'age': 34, 'gender': 'female'}"
3234,zzzz,"{'age': 55, 'gender': 'male'}"
- 用
pd.read_csv
and use theconverters
parameter withast.literal_eval
读取文件,这会将Information
列中的数据从str
类型转换为dict
类型。 - 使用
pd.json_normalize
解压dict
键作为列 headers 和行中的值 .join
具有df
的规范化列
.drop
Information
列
import pandas as pd
from ast import literal_eval
df = pd.read_csv('test.csv', converters={'Information': literal_eval})
df = df.join(pd.json_normalize(df.Information))
df.drop(columns=['Information'], inplace=True)
# display(df)
ID Name age gender
0 1234 xxxx 25 male
1 2234 yyyy 34 female
2 3234 zzzz 55 male
如果数据不是来自 csv 文件
import pandas as pd
from ast import literal_eval
data = {'ID': [1234, 2234, 3234],
'Name': ['xxxx', 'yyyy', 'zzzz'],
'Information': ["{'age': 25, 'gender': 'male'}", "{'age': 34, 'gender': 'female'}", "{'age': 55, 'gender': 'male'}"]}
df = pd.DataFrame(data)
# apply literal_eval to Information
df.Information = df.Information.apply(literal_eval)
# normalize the Information column and join to df
df = df.join(pd.json_normalize(df.Information))
# drop the Information column
df.drop(columns=['Information'], inplace=True)