简体   繁体   English

Pandas DataFrame,列中的列

[英]Pandas DataFrame, columns within a column

Here below is the CSV file that I'm working with: 以下是我正在使用的CSV文件:

表

I'm trying to get my hands on the enj coin: (United States) column. 我正在尝试使用enj coin: (United States)专栏。 Nonetheless when I try printing all of the columns of the DataFrame it doesn't appear to be treated as a column 但是,当我尝试打印DataFrame的所有列时,它似乎没有被视为一列

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. 我尝试使用df['Category: All categories']['enj coin: (United States)']访问该列,但不幸的是它不起作用。

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) ? 问题:有人可以向我解释一下如何将这个DataFrame(只有一个列Category: All categories )转换为一个包含Time and enj coin两列的DataFrame enj coin: (United States)

Thank you very much for your help 非常感谢您的帮助

Try using the parameter skiprows=2 when reading in the CSV. 读取CSV时,尝试使用参数skiprows=2 Ie

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

The csv looks good. CSV看起来不错。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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