简体   繁体   English

我在 pandas dataframe 列中有字典作为值。 我想将键列和值作为列值

[英]I have dictionary as value in pandas dataframe columns. I want to make the keys columns and values as column value

So i'm working on a dataframe which has a key-value pair as its value in columns.所以我正在研究一个 dataframe ,它有一个键值对作为列中的值。 Is there a way to make the keys as column name while only keeping the value left in the column.有没有办法将键作为列名,同时只保留列中的值。

Currently i have something like this:目前我有这样的事情:

    >0                          1                       2   
    >{'1536235175000': 26307.9} {'1536235176000': 0}    {'1536236701000': 2630} 
    >{'1536239919000': 1028127} {'1536239921000': 0}    NaN 
    >{'1536242709000': 2629.6}  {'1536242711000': 0}    NaN 

If you want to keep the row index, you can agg every row as as list and explode them.如果要保留行索引,可以将每一行聚合为列表并展开它们。

obj = df.apply(lambda x: list(x), axis=1).explode().dropna()
dfn = pd.DataFrame(obj.tolist(), index=obj.index)
dfn.stack().unstack()
#        1536235175000  1536235176000  1536236701000  1536239919000  \
# 0            26307.9            0.0         2630.0            NaN   
# 1                NaN            NaN            NaN      1028127.0   
# 2                NaN            NaN            NaN            NaN   

#        1536239921000  1536242709000  1536242711000  
# 0                NaN            NaN            NaN  
# 1                0.0            NaN            NaN  
# 2                NaN         2629.6            0.0

Check with concatconcat检查

pd.concat([pd.Series(df[x].tolist()) for x in df.columns], keys=df.columns, axis=1)

If lakes is your DataFrame, you can do something like如果湖泊是您的 DataFrame,您可以执行类似的操作

area_dict = dict(zip(lakes.area, lakes.count) ) area_dict = dict(zip(lakes.area, lakes.count) )

暂无
暂无

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

相关问题 我在其中 1 列中有一个带有 json 子字符串的数据框。 我想提取变量并为它们制作列 - I have a dataframe with a json substring in 1 of the columns. i want to extract variables and make columns for them Python 如何使用多个 pandas dataframe 列中的值作为元组键和单个列作为值来创建字典 - Python how to create a dictionary using the values in multiple pandas dataframe columns as tuple keys and a single column as value 将 Dataframe 中字典的值转换为列。 还使用其他值添加额外的列 - Convert value of dictionary in a Dataframe to columns. Also add extra columns using the other values 如何使字典键是 Pandas 数据框的一列到列? - How to make dictionary keys is one column of Pandas dataframe to the columns? 如何将数据框列中的值替换为另一个数据框列。 就像图片一样 - How could I replace the values from a dataframe column into another dataframe columns. Just like the image 我想根据 pandas 数据框行创建一个新列,该行在多列中具有相同的值,在 1 列中具有不同的值 - I want to make a new column based on pandas dataframe rows having equal values in multiple columns and different values in 1 column 我有一个包含多列的数据框。 如何按年份对列进行分组,并添加一个新列来对其中一列的值进行排名? - I have a data frame with multiple columns. How do I group the columns by year, and add a new column that ranks the values of one of the columns? 我想使用其他列中的值替换数据框中的一部分列值 - I want to replace part of a column value in a dataframe using values from other columns pyspark数据框到字典:列作为键和列值列表ad dict值 - pyspark dataframe to dictionary: columns as keys and list of column values ad dict value 在pandas数据框中,我想基于将其他列过滤为某些值来为该列分配值 - In a pandas dataframe I would like to assign a value to a column based on filtering other columns to certain values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM