nietsnegttiw nietsnegttiw - 1 year ago 59
Python Question

How To Create Custom DataFrame From Subset Of CSV File

I have a CSV file that looks like this:

1, 2, 3, 4, 5 ...
0, 1, 0, 1, 0 ...
0, 1, 0, 1, 0 ...
str1, str2, str3, str4, str5 ...
val1, val1.1, val1.2, val1.3, val1.4 ...
val2, val2.1, val2.2, val2.3, val2.4 ...

and I want to generate a dataframe that looks like this:

str2, str5
val1.1 val2.1
val2.1 val2.2

Here is my attempt:

for f in files:
data = pd.read_excel(f)
df = df.append(data)

df[5:10] //only care about values in rows below [str1, str2, ..., strn].
d = df.ix[:, '2' : '5']
d.columns = ['str2', 'str3', 'str4', 'str5'] //rename columns, reduce table size.

this produces:

str2 str3 str4 str5
val1.1 val1.2 val1.3 val1.4 ...

How do I eliminate
to get my original intended dataframe?

Answer Source

You can simply delete any undesired columns. Check out this discussion on deleting a Pandas DataFrame column: Delete column from pandas DataFrame

Then cruise on over to the documentation:

df = df.drop('str3', 1)
df = df.drop('str4', 1)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download