简体   繁体   English

python pandas dataframe:当默认值是可迭代的时,使用默认值创建新列

[英]python pandas dataframe: Creating new column with default value, when default value is an iterable

I have a pandas dataframe as below: 我有一个熊猫数据框,如下所示:

In  [1]: import pandas as pd
In  [2]: df = pd.DataFrame([[1,2],[3,4],[5,6]], columns=['a','b'])
In  [3]: print df
Out [3]: 
   a  b
0  1  2
1  3  4
2  5  6

Now I want to add a new column 'c' with a default value as a dictionary. 现在,我想添加一个默认值作为字典的新列“ c”。 The resulting dataframe should look like this: 结果数据框应如下所示:

   a  b             c
0  1  2  {1: 2, 3: 4}
1  3  4  {1: 2, 3: 4}
2  5  6  {1: 2, 3: 4}

I tried the following: 我尝试了以下方法:

df.at[:, 'c'] = {1: 2, 3: 4}
ValueError: Length of values does not match length of index

and

df['c'] = {1: 2, 3: 4}
ValueError: Must have equal len keys and value when setting with an iterable

This one works for me 这个对我有用

df['c'] = df.apply(lambda x: {1:2, 3:4}, axis=1)

but looks like a dirty approach. 但是看起来很肮脏。 Is there a cleaner way to do this? 有没有更清洁的方法可以做到这一点?

You have three rows in your DF and only two elements in your dict, do: 在DF中有三行,而在dict中只有两个元素,请执行以下操作:

c = {0:1,1:1,2:2}
df['c'] = c

Output: 输出:

 a  b  c
0  1  2  0
1  3  4  1
2  5  6  2

To have the same dictionary repeated along your dataframe you need to create a list of such dicts 要在数据框中重复使用相同的字典,您需要创建此类字典的列表

c = {1:2,3:4}
c = [c]*3
df['c'] = c

Output 产量

 a  b             c
0  1  2  {1: 2, 3: 4}
1  3  4  {1: 2, 3: 4}
2  5  6  {1: 2, 3: 4}

It is possible, but not recommended store dicts in column of DataFrame , because all vectorized pandas functions cannot be used: 可能但不建议将DataFrame存储在DataFrame列中,因为不能使用所有矢量化的熊猫函数:

df['c'] = [{1: 2, 3: 4} for x in np.arange(len(df))]
print (df)
   a  b             c
0  1  2  {1: 2, 3: 4}
1  3  4  {1: 2, 3: 4}
2  5  6  {1: 2, 3: 4}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM