[英]How to rename the rows in dataframe using pandas read (Python)?
I want to rename rows in python program (version - spyder 3
- python 3.6
) .我想重命名 python 程序中的行(版本spyder 3
- python 3.6
)。 At this point I have something like that:在这一点上,我有这样的事情:
import pandas as pd
data = pd.read_csv(filepath, delim_whitespace = True, header = None)
Before that i wanted to rename my columns:在此之前,我想重命名我的列:
data.columns = ['A', 'B', 'C']
It gave me something like that.它给了我类似的东西。
A B C
0 1 n 1
1 1 H 0
2 2 He 1
3 3 Be 2
But now, I want to rename rows.但现在,我想重命名行。 I want:我想:
A B C
n 1 n 1
H 1 H 0
He 2 He 1
Be 3 Be 2
How can I do it?我该怎么做? The main idea is to rename every row created by pd.read
by the data in the B column.主要思想是用 B 列中的数据重命名pd.read
创建的每一行。 I tried something like this:我试过这样的事情:
for rows in data:
data.rename(index={0:'df.loc(index, 'B')', 1:'one'})
but it's not working.但它不起作用。
Any ideas?有任何想法吗? Maybe just replace the data frame rows by column B?也许只是用 B 列替换数据框行? How?如何?
I think need set_index
with rename_axis
:我认为需要set_index
和rename_axis
:
df1 = df.set_index('B', drop=False).rename_axis(None)
Solution with rename
and dictionary: rename
和字典的解决方案:
df1 = df.rename(dict(zip(df.index, df['B'])))
print (dict(zip(df.index, df['B'])))
{0: 'n', 1: 'H', 2: 'He', 3: 'Be'}
If default RangeIndex
solution should be:如果默认RangeIndex
解决方案应该是:
df1 = df.rename(dict(enumerate(df['B'])))
print (dict(enumerate(df['B'])))
{0: 'n', 1: 'H', 2: 'He', 3: 'Be'}
Output :输出:
print (df1)
A B C
n 1 n 1
H 1 H 0
He 2 He 1
Be 3 Be 2
EDIT:编辑:
If dont want column B
solution is with read_csv
by parameter index_col
:如果不希望B
列解决方案是通过参数index_col
使用read_csv
:
import pandas as pd
temp=u"""1 n 1
1 H 0
2 He 1
3 Be 2"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), delim_whitespace=True, header=None, index_col=[1])
print (df)
0 2
1
n 1 1
H 1 0
He 2 1
Be 3 2
I normally rename my rows in my dataset by following these steps.我通常按照以下步骤重命名数据集中的行。
import pandas as pd
df=pd.read_csv("zzzz.csv")
#in a dataframe it is hard to change the names of our rows so,
df.transpose()
#this changes all the rows to columns
df.columns=["","",.....]
# make sure the length of this and the length of columns are same ie dont skip any names.
#Once you are done renaming them:
df.transpose()
#We get our original dataset with changed row names.
just put colnames into "names"
when reading阅读时只需将 colnames 放入"names"
中
import pandas as pd
df = pd.read_csv('filename.csv', names=["colname A", "colname B"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.