1

I have a data frame name it df I want to have like its first and second colums(series) in variable x and y.

I would have done that by name of the column like df['A'] or df['B'] or something like that.

But problem here is that data is itself header and it has no name.Header is like 2.17 ,3.145 like that.

So my Question is:

a) How to name column and start the data(which starts now from head) right after the name ?

b) How to get particular column's data if we don't know the name or it doesn't have the name ?

Thank you.

7
  • This depends on how the data is read in. Give details on how the DataFrame is created. Commented Jul 19, 2016 at 20:03
  • I read it in csv format pd.read_csv('kndkma') Commented Jul 19, 2016 at 20:05
  • May I know why downvote ? whoever did. Commented Jul 19, 2016 at 20:06
  • if your csv does not contain a header, you can use the names attribute to set the header manually. See pandas.pydata.org/pandas-docs/stable/generated/… for details. Commented Jul 19, 2016 at 20:07
  • I didn't donwvote your question, but I thought the formatting was negligent and somewhat misleading (the or looked like an operator). Fixed it to what I think you meant. Commented Jul 19, 2016 at 20:09

3 Answers 3

2

You might want to read the documentation on indexing.

For what you specified in the question, you can use

x, y = df.iloc[:, [0]], df.iloc[:, [1]]
Sign up to request clarification or add additional context in comments.

Comments

2

Set the names kwarg when reading the DataFrame (see the read_csv docs.

So instead of pd.read_csv('kndkma') use pd.read_csv('kndkma', names=['a', 'b', ...]).

3 Comments

The documentation recommends to also set header=None| names: "List of column names to use. If file contains no header row, then you should explicitly pass header=None"
@cel It seems redundant.
@ayhan, yes I don't think at the moment it is strictly required - yet if the documentation explicitly suggests so, I would also use it.
0

It is usually easier to name the columns when you read or create the DataFrame, but you can also name (or rename) the columns afterwards with something like:

df.columns = ['A','B', ...]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.