如何遍历给定特定列的行

时间:2019-10-02 15:24:05

标签: python pandas

我想遍历具有特定列的行,并将该单元格的值仅放入数组中。但是当我这样做时,我得到了一些奇怪的结构。

我尝试过不同的循环,但仍然以相同的方式出现

s_num = []
file = pd.ExcelFile('/Users/vhim/Documents/Sunjeev/Sample_Data.xlsx')
info = pd.read_excel(file, "Sheet1", header=0, usecols=['so_number'])

for i in info.iterrows():
    s_num.append(i)

我希望它仅在我的数组中输入单元格值,而是输入这些值

(0, so_number    107819563
Name: 0, dtype: int64)
(1, so_number    347905182
Name: 1, dtype: int64)
(2, so_number    108161659
Name: 2, dtype: int64)

2 个答案:

答案 0 :(得分:0)

尝试一下:

import {createDrawerNavigator, DrawerNavigatorItems} from "react-navigation-drawer";
import ProfileNavigator from "../Profile/Profile";
import Colors from "../../constants/colors";
import {AsyncStorage, Button, SafeAreaView, View} from "react-native";
import React from "react";
import {Logout} from "../Common";
import HomeNavigator from "../Home/Home";

const AppDrawerNavigator = createDrawerNavigator(
    {
        Profile: ProfileNavigator
    },
    {
        contentOptions: {
            activeTintColor: Colors.primary
        },
        contentComponent: props => {
            console.log(props) // THIS IS THE ISSUE CAUSING THE ERROR!!!!!!
            return (
                <View style={{ flex: 1, paddingTop: 20 }}>
                    <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
                        <DrawerNavigatorItems {...props} />
                        <Button
                            title="Logout"
                            color={Colors.primary}
                            onPress={Logout}
                        />
                    </SafeAreaView>
                </View>
            )
        },
        drawerType: 'slide',
        unmountInactiveRoutes: true
    }
)

export default AppDrawerNavigator

使用for i in df_temp["column1"]: print(i) 时得到的是对象元组,第一项是行索引,第二项是该列中的项目。

iterrows()

答案 1 :(得分:0)

之所以发生这种情况,是因为熊猫中的每一行本质上都是一个元组(可以说是列值的集合)。因此,您需要做的是使用类似

的内容访问每一行中的列
s_num.append(i['column_name'])

查看[this](https://medium.com/dunder-data/selecting-subsets-of-data-in-pandas-6fcd0170be9c)链接以获取更多信息。