大熊猫添加新专栏?

时间:2016-11-06 09:54:24

标签: python pandas

我正在学习Pandas并尝试学习其中的东西。但是,由于新列具有较大的索引号,因此我在添加新列时遇到困难。我希望添加3列以上。

这是我的代码:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

example=pd.read_excel("C:/Users/ömer sarı/AppData/Local/Programs/Python/Python35-32/data_analysis/pydata-book-master/example.xlsx",names=["a","b","c","d","e","f","g"])

dropNan=example.dropna()
#print(dropNan)
Fillup=example.fillna(-99)
#print(Fillup)
Countt=Fillup.get_dtype_counts()
#print(Countt)
date=pd.date_range("2014-01-01","2014-01-15",freq="D")
#print(date)
mm=date.month
yy=date.year
dd=date.day
df=pd.DataFrame(example)
print(df)
x=[i for i in yy]
print(x)
df["year"]=df[x]

这里是示例数据集:

     a      b      c   d   e      f     g
0    1    1.0    2.0   5   3   11.0  57.0
1    2    4.0    6.0  10   6   22.0  59.0
2    3    9.0   12.0  15   9   33.0  61.0
3    4   16.0   20.0  20  12   44.0  63.0
4    5   25.0    NaN  25  15    NaN  65.0
5    6    NaN   42.0  30  18   66.0   NaN
6    7   49.0   56.0  35  21   77.0  69.0
7    8   64.0   72.0  40  24   88.0  71.0
8    9   81.0    NaN  45  27   99.0  73.0
9   10    NaN  110.0  50  30    NaN  75.0
10  11  121.0    NaN  55  33  121.0  77.0
11  12  144.0  156.0  60  36  132.0   NaN
12  13  169.0  182.0  65  39  143.0  81.0
13  14  196.0    NaN  70  42  154.0  83.0
14  15  225.0  240.0  75  45  165.0  85.0

这是错误消息:

IndexError:指数超出范围

之后,我尝试了那个,我得到了新的错误:

df=pd.DataFrame(range(len(x)),index=x, columns=["a","b","c","d","e","f","g"])

pandas.core.common.PandasError: DataFrame constructor not properly called!

这只是一个学习的试验,如何将分割部分的日期添加为新栏目,如[“日期”,“年”,“月”,“日”......]

0 个答案:

没有答案