gpt4 book ai didi

My dataframe keeps making columns with name 'Unnamed' [duplicate](我的数据框一直在生成名为‘unname’[复制]的列)

转载 作者:bug小助手 更新时间:2023-10-24 23:34:34 24 4
gpt4 key购买 nike




import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np

df = pd.read_csv(r"C:\Users\bossd\OneDrive\Документы\datarn.csv")
df.drop(columns = df.columns[0], axis = 1)
display(df)

Everytime i want to delete the first column which always gets named 'Unnamed' and appears everytime i want to save changes with

每次我想要删除第一列时,它总是被命名为‘UNNAME’,并在每次我想要保存更改时出现


df.to_csv(r"C:\Users\bossd\OneDrive\Документы\datarn.csv")

And after i do this the column reappers or just doesn't delete, how do i delete these unnamed rows and save changes to a dataframe to be completely clear from these columns and have 'timestamp' as first column.

在我这样做之后,列将收获或不删除,我如何删除这些未命名的行并保存对数据帧的更改,以便完全从这些列中清除并将‘Timestamp’作为第一列。


(https://i.stack.imgur.com/Q66ha.png)

(https://i.stack.imgur.com/Q66ha.png))


Tried doing

试着去做


df = df.drop(columns = df.columns[0], axis = 1)

but it doesn't help me with this problem and the columns keep appearing

但这对我解决这个问题没有帮助,专栏不断出现


Also whenever i open the csv file i get this additional column that didn't exist before

此外,每当我打开CSV文件时,我都会收到以前不存在的附加列


(https://i.stack.imgur.com/jJ9WN.png)

(https://i.stack.imgur.com/jJ9WN.png))


更多回答

"Everytime i want to delete the first row" do you mean the first column?

“每次我都想删除第一行”你是说第一列吗?

Yeah, column, fixed

是,列,已修复

You shouldn't specify both columns= and axis=1. It's one or the other. Try this. df = df.drop(columns = ["Unnamed"])

您不应该同时指定COLUMNS=和AXIS=1。只能指定其中一个。尝尝这个。Df=df.drop(Columns=[“uname”])

doesn't work, deleted 1 column but doesn't delete further

不起作用,已删除1列,但不会进一步删除

Are there multiple columns that start with Unnamed? What further needs deleted?

是否有多个以未命名开头的列?还有什么需要删除的?

优秀答案推荐

What happens is when you are saving DF by default it adds an index column without a title hence the unmaned0 column appears in your DF when loading it again as this solution suggest you can delete multiple column with the same name but I'd like to add that when saving the DF to CSV -or any file format-
You can add index=False to not have an index column
for example:

发生的情况是,当您保存DF时,默认情况下它会添加一个没有标题的索引列,因此当再次加载它时,unmaned0列会出现在您的DF中,因为此解决方案建议您可以删除多个同名的列,但我想补充的是,在将DF保存为CSV或任何文件格式时,您可以添加index=False以没有索引列,例如:


df.to_csv(r"C:\Users\bossd\OneDrive\Документы\datarn.csv",index=False)


Solution


To drop multiple columns whose column name starts with 'Unnamed'

删除列名以“uname”开头的多个列


cols_to_drop = df.columns[df.columns.str.startswith('Unnamed')]

df.drop(cols_to_drop, axis=1, inplace=True)

更多回答

Holy sh-, thank you so much pal, you have one of the solutions to my problems, the person above fixed the unnamed columns and you fixed the first column reappearing. Thank you so much/

天哪,太感谢你了,朋友,你有我的问题的解决方案之一,上面的人修复了未命名的专栏,你修复了第一个专栏的重新出现。太感谢你了/

This will take care of all of them at once.

这将一下子照顾到他们所有人。

It actually did take care, but everytime i run df.to_csv(path) display(df) the unnamed columns reappear

它确实注意到了这一点,但每次我运行df.to_csv(Path)Display(Df)时,未命名的列都会重新出现

Right, as explained in the other answer, you continually regenerate the Unnamed columns based on the way you save and load your dataframes. My answer explained only how to purge them which was your question.

对,正如在另一个答案中所解释的,您可以根据保存和加载数据帧的方式不断地重新生成未命名的列。我的回答只解释了如何清除它们,这是你的问题。

Sorry for not hitting your answer the correct one, can't hit multiple of them, you still helped me out greatly, appreciated!

很抱歉没有命中你的正确答案,不能命中多个答案,你仍然帮了我很大的忙,谢谢!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com