![](/img/trans.png)
[英]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.