简体   繁体   English

Pandas,从 df 创建字典,用一列替换另一列

[英]Pandas, create dictionary from df, with one column as replacing another

I have an unknown number of DataFrames.我有未知数量的 DataFrame。 two for example:两个例如:

         date  week_score  daily_score site_name
0  2014-07-04         100           90    demo 2
1  2014-07-05          80           55    demo 2
2  2015-07-06          70           60    demo 2
         date  week_score  daily_score site_name
0  2014-07-04          85          100    demo 1
1  2014-07-05          50           80    demo 1
2  2015-07-06          45           30    demo 1

I know the data frames all have the same shape and columns names.我知道数据框都具有相同的形状和列名。

I want to combine them into a list of dictionaries ( df.to_dict(orient='records' ) but have the site_name as key and to do this for every score.我想将它们组合成一个字典列表( df.to_dict(orient='records' ),但将 site_name 作为键并为每个分数执行此操作。

the desired output is a bit tricky:所需的 output 有点棘手:

{'week_score: [{'date': '2014-07-04', 'demo 2': 100, 'demo 1': 85},
               {'date': '2014-07-05', 'demo 2': 80, 'demo 1': 50}, 
               {'date': '2014-07-06', 'demo 2': 70, 'demo 1': 45}],

'daily_score: [{'date': '2014-07-04', 'demo 2': 90, 'demo 1': 100},
               {'date': '2014-07-05', 'demo 2': 55, 'demo 1': 80}, 
               {'date': '2014-07-06', 'demo 2': 60, 'demo 1': 30}],

}


you can try this code:你可以试试这段代码:

d = dict()
for col in df.columns[1:-1].tolist():
    new_df = pd.DataFrame({'date':dfs[0]['date']})
    for df in dfs:
        site_name = df['site_name'].unique()[0]
        dropped = df.drop('site_name',axis='columns')
        new_df[site_name] = df[col]
    d[col] = new_df.to_dict('records')
>>>d

output: output:

{'week_score': [{'date': '2014-07-04', 'demo1': 85, 'demo2': 100},
  {'date': '2014-07-05', 'demo1': 50, 'demo2': 80},
  {'date': '2015-07-06', 'demo1': 45, 'demo2': 70}],
 'daily_score': [{'date': '2014-07-04', 'demo1': 100, 'demo2': 90},
  {'date': '2014-07-05', 'demo1': 80, 'demo2': 55},
  {'date': '2015-07-06', 'demo1': 30, 'demo2': 60}]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas.DataFrame:创建一个新列,使用当前df中的一列并在另一个df中查找一列,并进行计算 - Pandas.DataFrame: Create a new column, using one column from current df and by looking up one column in another df, with calculation pandas df 迭代一个 df 列 qty 并以 fifo 为基础从另一个 df 列分配 qty - pandas df iterate over one df column qty and allocate qty from another df column with fifo basis 如何通过 pandas 将特定列从一个 df 复制/粘贴到另一个 - How to copy/paste particular column from one df to another by pandas 从字典中创建熊猫df,但格式特殊 - create pandas df from dictionary but in a special format 如果 df1 column1 中的值与列表中的值匹配,Pandas 从另一个 df1 column2 在 df2 中创建新列 - Pandas create new column in df2 from another df1 column2 if a value in df1 column1 matches value in a list 熊猫将一个df中的空Datetime值替换为另一个中的时间戳值 - Pandas Replacing a Null Datetime Value in one df with a Timestamp Value from Another 熊猫df到字典的值作为从df列聚合的python列表 - Pandas df to dictionary with values as python lists aggregated from a df column 用一列和 100 行创建 pandas df - Create pandas df with one column and 100 rows Pandas:检查一个df中的值是否存在于另一个DF的任何列中 - Pandas: Check if value in one df exists in any column of another DF 从另一个具有字典键的列创建pandas dataframe列 - Create pandas dataframe column from another column that has dictionary keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM