Python:不排除map(dictionary)之后的行 [英] Python: do not exclude rows after map(dictionary)

查看:85
本文介绍了Python:不排除map(dictionary)之后的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用字典以便在数据框中创建新的字段/列.如果值不匹配,我希望将值设置为"NA"或类似的值.

I want to use a dictionary in order to create a new field/column in a dataframe. If the values don't match, I want the value to be set to 'NA' or something similar.

所以,我正在使用类似这样的东西:

So, I am using something like this:

#Creation of a dictionary after combining two lists
country_Codes = ['us', 'de', 'fr']
values        = ['US', 'EU', 'EU']
dictionary = dict(zip(country_Codes, values))

df['new'] = df['Country_Code'].map(dictionary, na_action=None)

,当我将所有内容导出到CSV时,它通常会创建新列.问题在于它会跳过不匹配的行.如果字典中没有匹配项,我的脚本将排除一些行.我认为问题与这条线有关: na_action='ignore'

and it creates the new column normally when I export everything to a CSV. The problem is that it skips the non-matched rows. My script excludes some rows if there is no match from the dictionary. I thought the issue was related to this line: na_action='ignore'

这是我在jupyter中使用的示例代码,在其中我临时创建了s和d系列:

Here is an example code I used in jupyter where I created temporarily the s and d series:

s = pd.Series(['us', 'de', 'random1','random2', 'fr', 'random3'])
d = pd.Series(['us', 'de', 'random1','random2', 'fr', 'random3'])

#Creation of a dictionary after combining two lists
country_Codes = ['us', 'de', 'fr']
values        = ['US', 'EU', 'EU']
dictionary = dict(zip(country_Codes, values))

a = s.map(dictionary, na_action=None)
b = d.map(dictionary, na_action='ignore')

这是我在打印后看到的内容:

and this is what I can see after printing:

我的第一个问题是如何看到相同的结果?似乎na_action不适用.其次,如何将行保留在具有多列的更复杂的数据框中?在打印type()之后,我检查了类型是否正确:

My first question is how do I see the same results? It seems as if the na_action does not apply. Secondly, how can I keep the rows in a more complex dataframe with multiple columns? I checked that the type is correct after printing the type():

class'pandas.core.frame.DataFrame'

class 'pandas.core.frame.DataFrame'

在此字段(Country_Code)与字典值不匹配之后,是否可以保留行?

Is there a way to keep the rows after there is no match for this field (Country_Code) with the dictionary values?

还有,是否可以打印字段(Country_Code)与字典值不匹配的行?

Also, is there a way to print the rows where the field (Country_Code) does not have a match with the dictionary values?

推荐答案

此处有几个问题:

(1)na_action

na_action与输入有关,与输出无关.这是从pandas文档中摘录的示例:

na_action relates to the input not the output. Here is an example lifted from pandas documentation:

>>> s = pd.Series([1, 2, 3, np.nan])
>>> s2 = s.map('this is a string {}'.format, na_action=None)
0    this is a string 1.0
1    this is a string 2.0
2    this is a string 3.0
3    this is a string nan
dtype: object

>>> s3 = s.map('this is a string {}'.format, na_action='ignore')
0    this is a string 1.0
1    this is a string 2.0
2    this is a string 3.0
3                     NaN
dtype: object

(2)如果不匹配,如何保留行?

(2) How to keep rows if no match?

这可能是您要寻找的.如果找不到匹配项,则不会更改.

This may be what you are looking for. If it can't find a match, it won't change.

b = d.replace(dictionary)

(3)打印在字典中Country_Code不匹配的行.

(3) Print rows where Country_Code does not have a match in dictionary.

df[~df['Country_Code'].isin(dictionary)]

这篇关于Python:不排除map(dictionary)之后的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆