0

I have a table where column names are not really organized like they have different years of data with different column numbers.

So I should access each data through specified column names. I am using this syntax to access a column.

df = df[["2018/12"]]

But when I just want to extract numbers under that column, using

df.iloc[0,0]

it throws an error like

single positional indexer is out-of-bounds

So I am using

df.loc[0]

but it has the column name with the numeric data.

How can I extract just the number of each row?

Below is the CSV data

    Closing Date,2014/12,2015/12,2016/12,2017/12,2018/12,Trend
Net Sales,"31,634","49,924","62,051","68,137","72,590",
""
Net increase,"-17,909","-16,962","-34,714","-26,220","-29,721",
Net Received,-,-,-,-,-,
Net Paid,-328,"-6,038","-9,499","-9,375","-10,661",
BangolPhoenix
  • 383
  • 1
  • 4
  • 16
  • 2
    Sorry, you are going to have to be a little clearer when describing the df. An extract would be useful as it is a little difficult to test answers with actual data. – Paula Thomas Jul 13 '19 at 15:34
  • 1
    you can refer [this](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) post and create an example dataframe and expected dataframe to clarify the issue – anky Jul 13 '19 at 15:37

1 Answers1

0

When writing this dumb question, I was just a beginner not even knowing what I wanted ask.

The OP's question comes down to "getting the row as a list" since he ended his post asking how to get numbers(though he said "number" maybe by mistake) of each row. The answer is that he made a mistake of using double square brackets in his example and it caused problems.

The solution is to use df = df["2018/12"] instead of df= df[["2018/12"]]

As for things I(me at the time of writing this) mentioned, I will answer them one by one:

Let's say the table looks like this

   Unnamed: 0      2018/12        country      drives_right
0         US           809  United States         True
1        AUS           731      Australia        False
2        JAP           588          Japan        False
3         IN            18          India        False
4         RU           200         Russia         True
5        MOR            70        Morocco         True
6         EG            45          Egypt         True

1>df = df[["2018/12"]] : it will output a dataframe which only has the column "2018/12" and the index column on the left side.

2>df.iloc[0,0] Now, since from 1> we have a new dataframe having only one column(except for index column mentioning index values) this will output the first element of the column. In the example above, the outcome will be "809" since it's the first element of the column.

3>

But when I just want to extract numbers under that column, using

df.iloc[0,0] -> doesn't make sense if you want to get extract numbers. It will just output one element 809 from the sub-dataframe you created using df = df[["2018/12"]].

it throws an error like

single positional indexer is out-of-bounds

Maybe you are confused about the outcome.(Maybe in this case "df" is the one before your df dataframe subset assignment?(df=df[["2018/12"]]) Since df = df[["2018/12"]] will output a dataframe so it will work fine.

3 So I am using

df.loc[0]

but it has the column name with the numeric data.

: Yes df.loc[0] from df = df[["2018/12"]] will return column name and the first element of that column.

4.

How can I extract just the number of each row?

You mean "numbers" of each row right?

Use this: print(df["2018/12"].values.tolist())

In terms of finding varying names of columns or rows, and then access each rows and columns, you should think of using regex.

BangolPhoenix
  • 383
  • 1
  • 4
  • 16