简体   繁体   English

根据列表熊猫在数据框中插入命名行

[英]Inserting named rows in dataframe based on a list pandas

I have a list (lets call it Y) of names and it's size is (1080,1). 我有一个名字列表(叫作Y),它的大小是(1080,1)。 I also have a dataframe of size(700,20) ( lets call it X) with the index containing names from the list Y. That is, there is no names in the dataframe that are not contained in Y, but there are names in Y that are not contained in the dataframe. 我也有一个size(700,20)的数据框(我们称它为X),索引包含列表Y中的名称。也就是说,数据框中没有Y中不包含的名称,但是数据框中未包含的Y。 Now what I would like to do is to insert empty rows containing the names in Y that are not already in X. Hence, I would like to add 380 new rows with empty values in all the columns and index values equal to the values of Y not already contained in the dataframe. 现在我想做的是插入包含Y中尚未包含X的名称的空行。因此,我想添加380个新行,所有列中的空值均与Y的值相等尚未包含在数据框中。

What about this: 那这个呢:

import pandas as pd
import numpy as np
df1 = pd.DataFrame({"name":["Marc", "Eric", "Bob"], "x":np.arange(3)})
Y = ["Marc", "Eric", "Bob","Carl"]
df2 = pd.DataFrame({"name":Y})
df = pd.concat([df1, df2[~df2["name"].isin(df1["name"])]])

Update I slightly modified my example to suit your case (except for first 4 characters instead of 10) 更新我略微修改了我的示例以适合您的情况(前4个字符代替10个字符除外)

import pandas as pd
import numpy as np
df1 = pd.DataFrame({"name":["Marc Green", "Eric White", "Carl Red"], "x":np.arange(3)})
Y = ["Marc", "Eric", "Anna","Carl"]
df2 = pd.DataFrame({"name":Y})
df = pd.concat([df1, df2[~df2["name"].isin(df1["name"].str[:4])]])

Update 2 It seems that in your case names in dataframe X are - separated so you can use this solution 更新2看来,在数据帧你的情况的名字X-分开,以便您可以使用此解决方案

df1 = pd.DataFrame({"name":["Marc - Green", "Eric - White", "Carl - Red"], "x":np.arange(3)})
Y = ["Marc", "Eric", "Anna","Carl"]
df2 = pd.DataFrame({"name":Y})
df = pd.concat([df1, df2[~df2["name"].isin(df1["name"].str.split("-").apply(lambda x:x[0].strip()))]])

Note as others already commented, when you ask a question is better if you provide an example of your data in text format so it's easy for other to help. 请注意,正如其他人已经评论过的那样,如果您以文本格式提供数据示例,那么在提问时会更好,这样其他人就可以轻松地获得帮助。

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

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