[英]Removing header column from pandas dataframe
I have the foll.我知道了。 dataframe: dataframe:
df df
A B
0 23 12
1 21 44
2 98 21
How do I remove the column names A
and B
from this dataframe?如何从此 dataframe 中删除列名A
和B
? One way might be to write it into a csv file and then read it in specifying header=None.一种方法可能是将其写入 csv 文件,然后在指定 header=None 时读取它。 is there a way to do that without writing out to csv and re-reading?有没有办法做到这一点而不写出 csv 并重新阅读?
I think you cant remove column names, only reset them by range
with shape
:我认为您不能删除列名,只能按shape
range
重置它们:
print df.shape[1]
2
print range(df.shape[1])
[0, 1]
df.columns = range(df.shape[1])
print df
0 1
0 23 12
1 21 44
2 98 21
This is same as using to_csv
and read_csv
:这与使用to_csv
和read_csv
相同:
print df.to_csv(header=None,index=False)
23,12
21,44
98,21
print pd.read_csv(io.StringIO(u""+df.to_csv(header=None,index=False)), header=None)
0 1
0 23 12
1 21 44
2 98 21
Next solution with skiprows
:下一个带有skiprows
解决方案:
print df.to_csv(index=False)
A,B
23,12
21,44
98,21
print pd.read_csv(io.StringIO(u""+df.to_csv(index=False)), header=None, skiprows=1)
0 1
0 23 12
1 21 44
2 98 21
How to get rid of a header(first row) and an index(first column).如何摆脱标题(第一行)和索引(第一列)。
To write to CSV file:写入 CSV 文件:
df = pandas.DataFrame(your_array)
df.to_csv('your_array.csv', header=False, index=False)
To read from CSV file:从 CSV 文件中读取:
df = pandas.read_csv('your_array.csv')
a = df.values
If you want to read a CSV file that doesn't contain a header, pass additional parameter header
:如果要读取不包含标题的 CSV 文件,请传递附加参数header
:
df = pandas.read_csv('your_array.csv', header=None)
我遇到了同样的问题,但以这种方式解决了它:
df = pd.read_csv('your-array.csv', skiprows=[0])
Haven't seen this solution yet so here's how I did it without using read_csv:还没有看到这个解决方案,所以这是我在不使用 read_csv 的情况下做到的:
df.rename(columns={'A':'','B':''})
If you rename all your column names to empty strings your table will return without a header.如果您将所有列名重命名为空字符串,您的表将返回而没有标题。
And if you have a lot of columns in your table you can just create a dictionary first instead of renaming manually:如果你的表中有很多列,你可以先创建一个字典,而不是手动重命名:
df_dict = dict.fromkeys(df.columns, '')
df.rename(columns = df_dict)
You can first convert the DataFrame to an Numpy array, using this:您可以首先使用以下方法将 DataFrame 转换为 Numpy 数组:
s1=df.iloc[:,0:2].values s1=df.iloc[:,0:2].values
Then, convert the numpy array back to DataFrame:然后,将 numpy 数组转换回 DataFrame:
s2=pd.DataFrame(s1) s2=pd.DataFrame(s1)
This will return a DataFrame with no Columns.这将返回一个没有列的 DataFrame。 enter image description here在此处输入图片说明
This works perfectly:这完美地工作:
To get the dataframe without the header use:要获得没有 header 的 dataframe,请使用:
totalRow = len(df.index)
df.iloc[1: totalRow]
Or you can use the second method like this:或者你可以像这样使用第二种方法:
totalRow = df.index.stop
df.iloc[1, totalRow]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.