根据键值关系在熊猫中创建新列

时间:2018-09-06 04:35:21

标签: python pandas

我有一个数据框,其中包含这样的序列:

0    CollgCr
1    Veenker
2    CollgCr
3    Crawfor
4    NoRidge
5    Mitchel
6    Somerst
7     NWAmes
8    OldTown
9    BrkSide

基于该系列,我想通过将它们分组来创建一个新列(功能)。

例如,如果数据为CollgCr或Veenker,则新列中的值为“中间”

我尝试使用以下代码

df_full['NeighborGrp'] = "Upper"
df_full['NeighborGrp'].loc[df_full["Neighborhood"] == "CollgCr"] = "Middle"
df_full['NeighborGrp'].loc[df_full["Neighborhood"] == ["Mitchel", "OldTown", "BrkSide", "Sawyer", "NAmes", "IDOTRR",
                                                          "MeadowV", "Edwards", "NPkVill", "BrDale", "SWISU", "Blueste"]] = "Lower"

第一行和第二行工作正常,但第三行返回错误“ ValueError: Arrays were different lengths"

padas中是否有任何特殊的语法可让我根据像这样的多值条件创建一个新列?

谢谢

3 个答案:

答案 0 :(得分:3)

使用np.selectpd.Series.isin

m = ['CollgCr', 'Veenker']
l = ["Mitchel", "OldTown", "BrkSide", "Sawyer", "NAmes", "IDOTRR","MeadowV", "Edwards", "NPkVill", "BrDale", "SWISU", "Blueste"]

df['new_col'] = np.select([df.city.isin(l), df.city.isin(m)], ['lower', 'middle'], default='upper')


    city    new_col
0   CollgCr middle
1   Veenker middle
2   CollgCr middle
3   Crawfor upper
4   NoRidge upper
5   Mitchel lower
6   Somerst upper
7   NWAmes  upper
8   OldTown lower
9   BrkSide lower

答案 1 :(得分:1)

如果您有一个可以表示城市和类型之间关系的表,merge将是一种更直接的方法(而不必对脚本中的每个城市进行硬编码):

In [52]: # In reality you probably should prepare the table elsewhere and read it in as a pandas dataframe
df_types = pd.DataFrame({'CollgCr': 'Middle',
                         'Veenker': 'Middle',
                         "Mitchel": 'Lower',
                         "OldTown": 'Lower',
                         "BrkSide": 'Lower',
                         "Sawyer": 'Lower',
                         "NAmes": 'Lower',
                         "IDOTRR": 'Lower',
                         "MeadowV": 'Lower',
                         "Edwards": 'Lower',
                         "NPkVill": 'Lower',
                         "BrDale": 'Lower',
                         "SWISU": 'Lower',
                         "Blueste": 'Lower'}, index=['Type']).T

df = pd.DataFrame({'city': ['CollgCr', 'Veenker', 'CollgCr', 'Crawfor',
                            'NoRidge', 'Mitchel', 'Somerst', 'NWAmes',
                            'OldTown', 'BrkSide']})

df.merge(df_types, left_on='city', right_index=True, how='left').fillna('Upper')

Out[52]:
      city    Type
0  CollgCr  Middle
1  Veenker  Middle
2  CollgCr  Middle
3  Crawfor   Upper
4  NoRidge   Upper
5  Mitchel   Lower
6  Somerst   Upper
7   NWAmes   Upper
8  OldTown   Lower
9  BrkSide   Lower

答案 2 :(得分:1)

MultiBinding的{​​{3}}与Series.map一起用于不匹配的值:

Text

或动态创建字典:

Bindings

environments.ts

这取决于数据,但是 export const thisIsA = (str: string) => `This is a ${str}`; 应该最快:

import * as env from `environments';

var text = "blah blah";
var strTest = env.thisIsA(text); //output: 'This is a blah blah'

计时代码:

const language = [
    { lang: "English", code: "en", icon: require(`../assets/us.png`) },
    { lang: "French", code: "fr", icon: require(`../assets/th.png`) },
    { lang: "Japanese", code: "jp", icon: require(`../assets/jp.png`) },
]

class App extends Component {
    onSelectLanguage = () => {
        return (
            language.map((data, i) => {
                return (
                    <View key={i} style={styles.dropDownView}>
                        <TouchableOpacity onPress={() => this.onSelectedLang(data)}>
                            <Text style={styles.dropDownText}><Image source="{data.icon}" />{data.lang}</Text>
                        </TouchableOpacity>
                    </View>
                )
            })
        )
    }
}