0

I want to put column names to a data set calling from a csv file. My code works perfectly fine follows.

DF2 = pd.DataFrame(data=DF1,index=SKU, 
   columns=['USER1','USER2','USER3','USER4','USER5','USER6'])

for 6 columns.

I have around 50 + columns and I want to read the column names from a csv file named USERID which is stored locally rather than typing the list. How can I do it?

The following code did not work

USERID = pd.read_csv("C:\EVALUATE\USERID.csv")
DF2 = pd.DataFrame(data=DF1,index=SKU, columns=USERID)

Any suggestions?

2
  • What is the output of print(USERID) ? Commented Apr 15, 2016 at 10:51
  • What is the format of the USERID.csv file? Is it a one line file where column names are given in the first row? Or they are at separate lines, one name per line. Commented Apr 15, 2016 at 10:52

1 Answer 1

2

Does the file have to be in CSV format?-- You could simply pipe the column names from standard input as a stream of whitespace-separated words by splitting the input lines and then chaining them together:

import fileinput
import itertools

USERID = itertools.chain(*(line.split() for line in fileinput.input()))
DF2 = pd.DataFrame(data=DF1,index=SKU, columns=USERID)

Then, given that you have a file USERID.txt which looks like this:

USER1 USER2
USER3 
USER4 USER5
USER6

...you can enter e.g. python DF2.py < USERID.txt either in a POSIX shell or in a Windows shell and list(USERID) would look like ['USER1','USER2','USER3','USER4','USER5','USER6'].

The only downside to this is that you couldn't have column names with whitespace in them but it would be easy to change this code and data format in order to accommodate that requirement.

Lastly, if, for some reason, you really don't want to pipe the data from standard input, you can read it directly in Python like so:

import itertools

with open("C:\EVALUATE\USERID.txt", "r") as USERID_instream:
    USERID = itertools.chain(*(line.split() for line in USERID_instream))
DF2 = pd.DataFrame(data=DF1,index=SKU, columns=USERID)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, all the 3 are good ideas. It worked perfect! Thank you!
If you really want to thank me, accept my answer as I'm currently trawling for some extra reputation points ;)

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.