[英]Pandas - Add Columns to a DataFrame Based in Dict from one of the Columns
I have the pandas.DataFrame
below: 我有下面的pandas.DataFrame
:
One of the columns from the Dataframe, pontos
, holds a dict for each of the rows. 数据框的列之一pontos
持有每一行的字典。
What I want to do is add one column to the DataFrame for each key from this dict. 我想要做的就是为此字典中的每个键向DataFrame添加一列。 So the new columns would be, in this example: rodada
, mes
, etc, and for each row, these columns would be populated with the respective value from the dict. 因此,在此示例中,新列将是: rodada
, mes
等,并且对于每一行,将使用dict中的相应值填充这些列。
So far I've tried the following for one of the keys: 到目前为止,我已经尝试了以下其中一个键:
df_times["rodada"] = [df_times["pontos"].get('rodada') for d in df_times["pontos"]]
However, as a result I'm getting a new column rodada
filled with None
values: 但是,结果是我得到了一个新列rodada
填充了None
值:
Any hints on what I'm doing wrong? 关于我在做什么错的任何提示吗?
You can create a new dataframe and concat
it to the current one like: 您可以创建一个新的数据帧和concat
它目前的一个,如:
df2 = pd.concat([df, pd.DataFrame(list(df.pontos))], axis=1)
import pandas as pd
df = pd.DataFrame([
['A', dict(col1='1', col2='2')],
['B', dict(col1='3', col2='4')],
], columns=['X', 'D'])
print(df)
df2 = pd.concat([df, pd.DataFrame(list(df.D))], axis=1)
print(df2)
X D
0 A {'col2': '2', 'col1': '1'}
1 B {'col2': '4', 'col1': '3'}
X D col1 col2
0 A {'col2': '2', 'col1': '1'} 1 2
1 B {'col2': '4', 'col1': '3'} 3 4
You just need a slight change in your comprehension to extract that data. 您只需要稍微改变一下理解即可提取该数据。
It should be: 它应该是:
df_times["rodada"] = [d.get('rodada') for d in df_times["pontos"]] df_times [“ rodada”] = [d.get('rodada')for d in df_times [“ pontos”]]
You want the values of the dictionary key 'rodada' to be the basis of your new column. 您希望字典键“ rodada”的值成为新列的基础。 So you iterate over those dictionary entries in the loop- in other words, d, and then extract the value by key to make the new column. 因此,您要循环遍历循环中的那些字典条目,即d,然后通过键提取值以创建新列。
您还可以使用join和pandas apply功能:
df=df.join(df['pontos'].apply(pd.Series))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.