使用JupyterNotebook从DataFrame到Excel

时间:2018-03-09 01:49:06

标签: python pandas dataframe jupyter-notebook

嗨我有这个从dataFrame到excel的示例数据。现在我想添加新列。我的问题是JupyterNotebook的新手,我正在学习。以下是我的代码。

import pandas as pd
import sys
print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)

# Create DataFrame
d = [1,2,3,4,5,6,7,8,9]
e = [10, 11, 12, 13, 14, 15]
df = pd.DataFrame(d, columns = ['Number'])
df

输出就是这个:

   Number
0   1
1   2
2   3
3   4
4   5
5   6
6   7
7   8
8   9

现在我添加了一个新的列数据,即列e。我将如何向其中添加新列?

1 个答案:

答案 0 :(得分:0)

d和e的形状不相同。添加的方式,我不怎么做。也许这样?

In [83]: import pandas as pd

In [84]: d = [1,2,3,4,5,6,7,8,9]

In [85]: e = [10, 11, 12, 13, 14, 15]

In [86]: df = pd.DataFrame(d, columns = ['Number'])

In [87]: dfe = pd.DataFrame(e, columns=["e"])

In [88]: pd.concat([df, dfe], axis=1)
Out[88]:
   Number     e
0       1  10.0
1       2  11.0
2       3  12.0
3       4  13.0
4       5  14.0
5       6  15.0
6       7   NaN
7       8   NaN
8       9   NaN

In [89]: pd.concat([df, dfe], axis=1, join="inner")
    ...:
Out[89]:
   Number   e
0       1  10
1       2  11
2       3  12
3       4  13
4       5  14
5       6  15