根据其内容重命名列
我看到了一些类似的问题,但不是我一直在寻找的问题。
我有一个递送给我的Excel表:
- 我需要的列是
- 第二行上的列名
- ,但是列的顺序可能会有所不同(有时数字是第一个或城市的名称为不同的顺序)
- 有2列带有相同的名称。
这是重复的方法:
data = [['name','name','number'], ['Nick','Brussels', 15], ['Tom','Paris', 14]]
df = pd.DataFrame(data)
我将第一行列出这样的列标题:
df=df.rename(columns=df.iloc[0]).drop(df.index[0])
我确定的是“布鲁塞尔”始终在数据集中,所以我的问题是:我可以基于基于列的名称而基于该列中的值。
因此,在伪代码中,这将是:
if the column contains the word 'Brussels' rename the column to 'city'
我到目前为止的所有尝试都更改两个列名称,因为当我选择包含布鲁塞尔的列时,它将“名称”作为更改的索引。我想拥有iLoc,返回...
我的目标是拥有:
name city number
1 Nick Brussels 15
2 Tom Paris 14
I saw some similar questions but not quite the one I was looking for.
I have a excel sheet that is delivered to me:
- The columns I need are there
- the column names are on the second row
- but the order of the columns may vary (sometimes the number comes first, or the city and names are in different order)
- there are 2 column with the same name.
This is how to replicate:
data = [['name','name','number'], ['Nick','Brussels', 15], ['Tom','Paris', 14]]
df = pd.DataFrame(data)
I make my first row the column headers like this:
df=df.rename(columns=df.iloc[0]).drop(df.index[0])
What I know for sure is that the value 'Brussels' is always in the dataset, so my question is : can I change the name of the column based on a value in that column.
so in pseudo code this would be :
if the column contains the word 'Brussels' rename the column to 'city'
All my attempts so far change both column names, because it returns 'name' as the index to change when I select the column containing Brussels. I would like to have the iloc, returned...
My goal is to have this:
name city number
1 Nick Brussels 15
2 Tom Paris 14
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用布尔蒙版来找到正确的列:
You can use a boolean mask to find the right column:
这类似于Corralien在使用布尔蒙版时的答案,但它不是列表理解,而是先修改源行,然后像您一样将其分配为列。
This is similar to Corralien's answer in using a boolean mask, but instead of a list comprehension, it modifies the source row first and then assigns it as columns like you did.