是否添加新列?

2024-10-01 04:52:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在学习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

以下是错误消息:

索引器:索引超出范围

在那之后,我试了一下,发现了新的错误:

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!

这只是一个学习的尝试,我怎样才能添加像[“date”,“year”,“month”,“day”…]这样的新列中的日期


Tags: importdataframepandasdfdateexampleasrange