在 Python 中将数组转换为 DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45264141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert array into DataFrame in Python
提问by feng_h
import pandas as pd
import numpy as np
e = np.random.normal(size=100)
e_dataframe = pd.DataFrame(e)
When I input the code above, I get this answer:
当我输入上面的代码时,我得到了这个答案:
But how do I change the column name?
但是如何更改列名?
回答by jezrael
You can add parameter columns or use dict
with key which is converted to column name:
您可以添加参数列或dict
与转换为列名的键一起使用:
np.random.seed(123)
e = np.random.normal(size=10)
dataframe=pd.DataFrame(e, columns=['a'])
print (dataframe)
a
0 -1.085631
1 0.997345
2 0.282978
3 -1.506295
4 -0.578600
5 1.651437
6 -2.426679
7 -0.428913
8 1.265936
9 -0.866740
e_dataframe=pd.DataFrame({'a':e})
print (e_dataframe)
a
0 -1.085631
1 0.997345
2 0.282978
3 -1.506295
4 -0.578600
5 1.651437
6 -2.426679
7 -0.428913
8 1.265936
9 -0.866740
回答by Christopher Matthews
In general you can use pandas rename function here. Given your dataframe you could change to a new name like this. If you had more columns you could also rename those in the dictionary. The 0 is the current name of your column
一般来说,您可以在此处使用 pandas 重命名功能。鉴于您的数据框,您可以更改为这样的新名称。如果您有更多列,您还可以重命名字典中的列。0 是您的列的当前名称
import pandas as pd
import numpy as np
e = np.random.normal(size=100)
e_dataframe = pd.DataFrame(e)
e_dataframe.rename(index=str, columns={0:'new_column_name'})