[英]Pandas Dataframe to Json using df.to_dict('records') changes a column on lists to strings
[英]Pandas: How to easily share a sample dataframe using df.to_dict()?
这个问题之前被标记为How to make good reproducible pandas examples的副本。 毫无疑问,该贡献应该是任何寻求制作此类可重复数据样本的人的首选帖子,而这篇文章旨在阐明一种非常实用且有效的方法,可以使用df.to_dict()
在问题中包含给定的数据样本结合df=pd.DataFrame(<dict>)
。 How to make good reproducible pandas examples中的问题和答案都没有明确涵盖这一点。 使用df.to_dict()
也可以很好地与df.to_clipboard()
一起使用,简明扼要地包含在如何使用 to_clipboard() 提供 DataFrame 的可复制副本中
尽管关于如何提出一个好问题有清晰简洁的指导? 和How to create a Minimal, Reproducible Example ,许多人似乎只是忽略了在他们的问题中包含可重现的数据样本。 那么当简单的pd.DataFrame(np.random.random(size=(5, 5)))
不够用时,有什么实用且简单的方法来重现数据样本呢? 例如,您如何使用df.to_dict()并在问题中包含 output?
在许多情况下,使用带有df.to_dict()
的方法可以完美地完成工作:以下是我想到的两种情况:
案例 1:您有一个 dataframe 从本地来源构建或加载到 Python
案例 2:您在另一个应用程序(如 Excel)中有一个表格
案例 1:您从本地源构建或加载了 dataframe
假设您有一个名为df
的 pandas dataframe ,只需
df.to_dict()
,并且pd.DataFrame(<output>)
并将该块包含在您现在可重现的代码片段中。案例 2:您在另一个应用程序(如 Excel)中有一个表格
根据来源和分隔符,如(',', ';' '\\s+')
后者表示任何空格,您可以简单地:
Ctrl+C
内容df=pd.read_clipboard(sep='\\s+')
,然后df.to_dict()
,并且df=pd.DataFrame(<output>)
中包含 output在这种情况下,您的问题的开头将如下所示:
import pandas as pd
df = pd.DataFrame({0: {0: 0.25474768796402636, 1: 0.5792136563952824, 2: 0.5950396800676201},
1: {0: 0.9071073567355232, 1: 0.1657288354283053, 2: 0.4962367707789421},
2: {0: 0.7440601352930207, 1: 0.7755487356392468, 2: 0.5230707257648775}})
当然,对于较大的数据帧,这会有点笨拙。 但很多时候,所有试图回答您问题的人都需要您的真实世界数据的一小部分样本,以考虑您的数据结构。
df.head(20).to_dict()
仅包含前20 rows
,并且df.to_dict('split')
(除了'split'
之外还有其他选项)将您的 output 重塑为需要更少行的 dict 来更改您的 dict 的格式。这是一个使用iris数据集的示例,以及 plotly express 提供的其他位置。
如果你只是运行:
import plotly.express as px
import pandas as pd
df = px.data.iris()
df.to_dict()
这将产生近 1000 行的 output,并且作为可重现的样本不太实用。 但是如果你包括.head(25)
,你会得到:
{'sepal_length': {0: 5.1, 1: 4.9, 2: 4.7, 3: 4.6, 4: 5.0, 5: 5.4, 6: 4.6, 7: 5.0, 8: 4.4, 9: 4.9},
'sepal_width': {0: 3.5, 1: 3.0, 2: 3.2, 3: 3.1, 4: 3.6, 5: 3.9, 6: 3.4, 7: 3.4, 8: 2.9, 9: 3.1},
'petal_length': {0: 1.4, 1: 1.4, 2: 1.3, 3: 1.5, 4: 1.4, 5: 1.7, 6: 1.4, 7: 1.5, 8: 1.4, 9: 1.5},
'petal_width': {0: 0.2, 1: 0.2, 2: 0.2, 3: 0.2, 4: 0.2, 5: 0.4, 6: 0.3, 7: 0.2, 8: 0.2, 9: 0.1},
'species': {0: 'setosa', 1: 'setosa', 2: 'setosa', 3: 'setosa', 4: 'setosa', 5: 'setosa', 6: 'setosa', 7: 'setosa', 8: 'setosa', 9: 'setosa'},
'species_id': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1}}
现在我们正在取得进展。 但是根据数据的结构和内容,这可能无法以令人满意的方式涵盖内容的复杂性。 但是您可以通过像这样包含to_dict('split')
来在更少的行中包含更多数据:
import plotly.express as px
df = px.data.iris().head(10)
df.to_dict('split')
现在您的 output 将如下所示:
{'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
'columns': ['sepal_length',
'sepal_width',
'petal_length',
'petal_width',
'species',
'species_id'],
'data': [[5.1, 3.5, 1.4, 0.2, 'setosa', 1],
[4.9, 3.0, 1.4, 0.2, 'setosa', 1],
[4.7, 3.2, 1.3, 0.2, 'setosa', 1],
[4.6, 3.1, 1.5, 0.2, 'setosa', 1],
[5.0, 3.6, 1.4, 0.2, 'setosa', 1],
[5.4, 3.9, 1.7, 0.4, 'setosa', 1],
[4.6, 3.4, 1.4, 0.3, 'setosa', 1],
[5.0, 3.4, 1.5, 0.2, 'setosa', 1],
[4.4, 2.9, 1.4, 0.2, 'setosa', 1],
[4.9, 3.1, 1.5, 0.1, 'setosa', 1]]}
现在您可以轻松地增加.head(10)
中的数字,而不会过多地混淆您的问题。 但有一个小缺点。 现在您不能再直接在pd.DataFrame
中使用输入。 但是如果你包含一些关于index, column, and data
的规范,你会很好的。 因此,对于这个特定的数据集,我首选的方法是:
import pandas as pd
import plotly.express as px
sample = {'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
'columns': ['sepal_length',
'sepal_width',
'petal_length',
'petal_width',
'species',
'species_id'],
'data': [[5.1, 3.5, 1.4, 0.2, 'setosa', 1],
[4.9, 3.0, 1.4, 0.2, 'setosa', 1],
[4.7, 3.2, 1.3, 0.2, 'setosa', 1],
[4.6, 3.1, 1.5, 0.2, 'setosa', 1],
[5.0, 3.6, 1.4, 0.2, 'setosa', 1],
[5.4, 3.9, 1.7, 0.4, 'setosa', 1],
[4.6, 3.4, 1.4, 0.3, 'setosa', 1],
[5.0, 3.4, 1.5, 0.2, 'setosa', 1],
[4.4, 2.9, 1.4, 0.2, 'setosa', 1],
[4.9, 3.1, 1.5, 0.1, 'setosa', 1],
[5.4, 3.7, 1.5, 0.2, 'setosa', 1],
[4.8, 3.4, 1.6, 0.2, 'setosa', 1],
[4.8, 3.0, 1.4, 0.1, 'setosa', 1],
[4.3, 3.0, 1.1, 0.1, 'setosa', 1],
[5.8, 4.0, 1.2, 0.2, 'setosa', 1]]}
df = pd.DataFrame(index=sample['index'], columns=sample['columns'], data=sample['data'])
df
现在您将拥有此 dataframe 可以使用:
sepal_length sepal_width petal_length petal_width species species_id
0 5.1 3.5 1.4 0.2 setosa 1
1 4.9 3.0 1.4 0.2 setosa 1
2 4.7 3.2 1.3 0.2 setosa 1
3 4.6 3.1 1.5 0.2 setosa 1
4 5.0 3.6 1.4 0.2 setosa 1
5 5.4 3.9 1.7 0.4 setosa 1
6 4.6 3.4 1.4 0.3 setosa 1
7 5.0 3.4 1.5 0.2 setosa 1
8 4.4 2.9 1.4 0.2 setosa 1
9 4.9 3.1 1.5 0.1 setosa 1
10 5.4 3.7 1.5 0.2 setosa 1
11 4.8 3.4 1.6 0.2 setosa 1
12 4.8 3.0 1.4 0.1 setosa 1
13 4.3 3.0 1.1 0.1 setosa 1
14 5.8 4.0 1.2 0.2 setosa 1
这将大大增加您获得有用答案的机会!
df_to_dict()
将无法读取像1: Timestamp('2020-01-02 00:00:00')
这样的时间戳,而不包括from pandas import Timestamp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.