根据其内容重命名列

发布于 2025-02-13 22:20:58 字数 788 浏览 4 评论 0原文

我看到了一些类似的问题,但不是我一直在寻找的问题。

我有一个递送给我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2025-02-20 22:21:02

您可以使用布尔蒙版来找到正确的列:

df.columns = [c if not m else 'city'
                  for c, m in zip(df.columns, df.eq('Brussels').any())]
print(df)

# Output
   name      city number
1  Nick  Brussels     15
2   Tom     Paris     14

You can use a boolean mask to find the right column:

df.columns = [c if not m else 'city'
                  for c, m in zip(df.columns, df.eq('Brussels').any())]
print(df)

# Output
   name      city number
1  Nick  Brussels     15
2   Tom     Paris     14
感性不性感 2025-02-20 22:21:02

这类似于Corralien在使用布尔蒙版时的答案,但它不是列表理解,而是先修改源行,然后像您一样将其分配为列。

df.iloc[0][df.eq('Brussels').any()] = 'city'
df = df.rename(columns=df.iloc[0]).drop(df.index[0])

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.

df.iloc[0][df.eq('Brussels').any()] = 'city'
df = df.rename(columns=df.iloc[0]).drop(df.index[0])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文