简体   繁体   中英

Pandas DataFrame, columns within a column

Here below is the CSV file that I'm working with:

表

I'm trying to get my hands on the enj coin: (United States) column. Nonetheless when I try printing all of the columns of the DataFrame it doesn't appear to be treated as a column

Code:

import pandas as pd

df = pd.read_csv("/multiTimeline.csv")
print(df.columns)

I get the following output:

Index(['Category: All categories'], dtype='object')

I've tried accessing the column with df['Category: All categories']['enj coin: (United States)'] but sadly it doesn't work.

Question: Could someone possibly explain to me how I could possibly transform this DataFrame (which has only one column Category: All categories ) into a DataFrame which has two columns Time and enj coin: (United States) ?

Thank you very much for your help

Try using the parameter skiprows=2 when reading in the CSV. Ie

df = pd.read_csv("/multiTimeline.csv", skiprows=2)

The csv looks good.

Ignore the complex header at the top.

pd.read_csv(csvdata, header=[1])

The entire header can be taken in as well, although it is not delimited as the data is.

import pandas as pd
from pandas.compat import StringIO
print(pd.__version__)

csvdata = StringIO("""Category: All categories

Time,enj coin: (United States)
2019-04-10T19,7
2019-04-10T20,20""")

df = pd.read_csv(csvdata, header=[0,1])
print(df)
0.24.2
              Category: All categories
                                  Time
2019-04-10T19                        7
2019-04-10T20                       20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM