如果列名称包含句点,如何更改列名称?
我正在尝试合并具有相同列但具有不同命名约定的文件。某些文件的列名称包含句点(“.”),而其他文件的列名称不包含句点。
有些文件如下所示:
First.Name | Last.Name |
---|---|
Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
而其他文件如下所示:
First Name | Last Name |
---|---|
Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
我只想更改列名称,如果列名称包含句点。我该怎么办?
这是我的代码片段
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=0)
if df.loc[:,df.columns.str.contains('.')].any() == True:
df.rename(columns = {'First.Name':'First Name', 'Last.Name':'Last Name'})
li.append(df)
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I am trying to merge files that have the same columns but have different naming conventions. Some files have column names that contain a period ('.'), while others have columns name that does not contain a period.
Some of the files look like this:
First.Name | Last.Name |
---|---|
Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
While others look like this:
First Name | Last Name |
---|---|
Cell 1 | Cell 2 |
Cell 3 | Cell 4 |
I only want to change the column names if the column names contain a period. How do I go about this?
This is a snippet of my code
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=0)
if df.loc[:,df.columns.str.contains('.')].any() == True:
df.rename(columns = {'First.Name':'First Name', 'Last.Name':'Last Name'})
li.append(df)
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要检查
.
。You don't need to check for
.
.我会做这样的事情:
让我知道这是否适合你。
I'd do something like this:
Let me know if that works for you.