Python 从值列表中添加或更新数据帧

Python 从值列表中添加或更新数据帧,python,pandas,Python,Pandas,我有一个数据帧 >>> df name count 0 a 1 1 b 2 2 c 0 我想使用此列表更新该值 l = ['a','b','d'] 所以我更新的df应该是这样的 >>> df name count 0 a 2 1 b 3 2 c 0 3 d 1 我能想到的唯一方法就是使用循环。你们能提出其他的建议吗。 谢谢,您可以

我有一个数据帧

>>> df
  name  count
0    a      1
1    b      2
2    c      0
我想使用此列表更新该值

 l = ['a','b','d']
所以我更新的df应该是这样的

>>> df
  name  count
0    a      2
1    b      3
2    c      0
3    d      1
我能想到的唯一方法就是使用循环。你们能提出其他的建议吗。 谢谢,您可以从列表中创建序列,并通过获取计数,然后通过和一起从df创建序列,最后供数据帧使用 以及:


另一种方法是将值彼此相加,然后再加上GroupBy.count:

输出


我认为在这些情况下使用计数器是合适的。 唯一的缺点是从df转换为dict,反之亦然

from collections import Counter

# initialize your variables
df = pd.DataFrame({'name': ['a', 'b', 'c'],
                   'count': [1, 2, 0]})
l = ['a', 'b', 'd']

# convert to dict with name - count pairs and update with counter of l
df_as_dict = dict(zip(df['name'].values, df['count'].values))
df_as_dict.update(Counter(df_as_dict) + Counter(l))

# create dataframe with updates values
new_df = pd.DataFrame({'name': list(df_as_dict.keys()), 
                       'count': list(df_as_dict.values())})
# ensure df format
new_df = new_df.sort_values('name').reset_index(drop=True)

new_df
输出
x = sorted(list(set(df['name'].tolist() + l)))
new = pd.concat([df['name'].repeat(df['count']).to_frame()
                 , pd.DataFrame({'name':l})]).groupby('name')['name'].count()
new = new.reindex(x, fill_value=0).reset_index(name='count')
print(new)
  name  count
0    a      2
1    b      3
2    c      0
3    d      1
from collections import Counter

# initialize your variables
df = pd.DataFrame({'name': ['a', 'b', 'c'],
                   'count': [1, 2, 0]})
l = ['a', 'b', 'd']

# convert to dict with name - count pairs and update with counter of l
df_as_dict = dict(zip(df['name'].values, df['count'].values))
df_as_dict.update(Counter(df_as_dict) + Counter(l))

# create dataframe with updates values
new_df = pd.DataFrame({'name': list(df_as_dict.keys()), 
                       'count': list(df_as_dict.values())})
# ensure df format
new_df = new_df.sort_values('name').reset_index(drop=True)

new_df
   count name
0      2    a
1      3    b
2      0    c
3      1    d