[英]merge multi-indexed column names on pandas dataframe
I have a csv file that has its column names separated over various lines, like this: 我有一个csv文件,其列名用各种行分隔,如下所示:
ID,Flow,Flow
,,Type
1,21.79,1
2,1129.32,0
So the column names are ID
, Flow
, and FLow Type
. 因此,列名是
ID
, Flow
和FLow Type
。 When I read this csv as a multi-indexed dataframe: 当我将此csv阅读为多索引数据框时:
df = pandas.read_csv(path, header = [0,1])
I end up with 我最终
ID Flow
Unnamed: 0_level_1 Unnamed: 1_level_1 Type
1 21.79 1
2 1129.32 0
Is there a simple way to merge the column names, so that I end up with a simple column index with their proper names? 有没有简单的方法来合并列名,这样我就可以得到一个简单的列索引以及它们的专有名称? In the end, I would like to have the equivalent of
最后,我想拥有相当于
pandas.DataFrame({'ID': [1,2], 'Flow': [21.79, 1129.32], 'Flow Type': [1,0]})
This method assumes you know the order to columns and their names, and number of rows you want to skip. 此方法假定您知道列的顺序及其名称,以及要跳过的行数。
If so, you could 如果是这样,您可以
pd.read_csv('temp.csv', skiprows=2, names=['ID', 'Flow', 'FLow Type'])
Here, column names are ['ID', 'Flow', 'FLow Type']
and we are skipping skiprows=2
from the 'temp.csv' file. 在这里,列名是
['ID', 'Flow', 'FLow Type']
,我们正在从'temp.csv'文件中跳过skiprows=2
。
That shall read the csv in intended way. 那将以预期方式读取csv。 However, this method woudn't work if column order and skiprows changes.
但是,如果列顺序和跳过行发生更改,则此方法将无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.