gpt4 book ai didi

python - 有没有办法从具有值列表的字典创建数据框?

转载 作者:太空宇宙 更新时间:2023-11-04 09:57:34 25 4
gpt4 key购买 nike

我有一个字典 myDict,我想使用这个 myDict 创建一个数据框 df,如下所示:

myDict = {
1: [''],
2: ['07/19/2017', ' 10/18/2007', '12/20/2002','12/20/2002' ],
3: ['07/19/2017', ' 10/18/2007'],
4: ['12/13/1993'],
5: [''],
6: ['08/01/2007'],
7: ['04/23/2007'],
8: ['02/06/2007'],
9: ['02/06/2007'],
10: ['11/08/2001'],
11: [''],
12: [''],
13: ['12/20/2002']
}

df
ID Col1 Col2 Col3 Col4
1
2 07/19/2017 10/18/2007 12/20/2002 12/20/2002
3 07/19/2017 10/18/2007
4 12/13/1993
5
6 08/01/2007
7 04/23/2007
8 02/06/2007
9 02/06/2007
10 11/08/2001
11
12
13 12/20/2002

我如何使这成为可能?谢谢。

将所有内容都放入一个函数中是行不通的...

def split_Date(df):
Dates1 = df.set_index('IDX')['Date'].to_dict()
dates = {}
for k, v in Dates1.items():
v = v.split(',')
dates[k] = [i for i in v]
dates = {k: sorted(v, key=lambda x: datetime.strptime(x.strip(), "%m/%d/%Y") if x != "" else x) for k, v in dates.items()}
df_dates = pd.DataFrame.from_dict(dates, orient="index").fillna('').rename_axis("IDX").rename(columns="Date{}".format).reset_index()
df = pd.merge(df, df_dates, on='IDX', how='inner', suffixes=('_chem', '_df'))
return df #Adding this doesn't make any difference

在函数外运行这段代码效果很好。但是,这需要我在每次有新的 data 时更改所有行中的 myData 的值。这不如拥有一个功能那么高效

Dates1 = myData.set_index('IDX')['Date'].to_dict()
dates = {}
for k, v in Dates1.items():
v = v.split(',')
dates[k] = [i for i in v]
dates = {k: sorted(v, key=lambda x: datetime.strptime(x.strip(), "%m/%d/%Y")
if x != "" else x) for k, v in dates.items()}
df_dates = pd.DataFrame.from_dict(dates, orient="index").fillna('').rename_axis("IDX").rename(columns="Date{}".format).reset_index()
myData = pd.merge(myData, df_dates, on='IDX', how='inner', suffixes=('_chem', '_df'))

最佳答案

您可以使用pd.DataFrame.from_dict 读取它并通过orient 参数将键设置为索引:

pd.DataFrame.from_dict(myDict, orient="index").fillna('')

# 0 1 2 3
#1
#2 07/19/2017 10/18/2007 12/20/2002 12/20/2002
#3 07/19/2017 10/18/2007
#4 12/13/1993
#5
#6 08/01/2007
# ...

要将键设置为单独的列,您可以使用 reset_index :

(pd.DataFrame.from_dict(myDict, orient="index")
.fillna('')
.rename_axis("ID")
.rename(columns="Col{}".format)
.reset_index())

# ID Col0 Col1 Col2 Col3
#0 1
#1 2 07/19/2017 10/18/2007 12/20/2002 12/20/2002
#2 3 07/19/2017 10/18/2007
#3 4 12/13/1993
#4 5
# ...

关于python - 有没有办法从具有值列表的字典创建数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45200397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com