Trying to create a dataframe in Python pandas.
data = {
'Date_Range': pd.date_range('20130101', periods=6),
'Series': pd.Series(data=["start", 2, 5.6, pd.datetime( 2020, 1, 31), True, "last"]),
'Int_Array': np.array([3, 7, 5, 2, 8, 1], dtype='int32'),
'Name': pd.Categorical(["John", "Kurt", "Carl", "Mary", "Alfred", "Tom"]),
'Constant': 'foobar'
}
df = pd.DataFrame(data, index=data["Date_Range"])
# df = pd.DataFrame(data)
print(df)
When I add the index, then the Series gives Nan, but without the index, there is no problem. Why is this happening?
Date_Range Series Int_Array Name Constant
2013-01-01 2013-01-01 NaN 3 John foobar
2013-01-02 2013-01-02 NaN 7 Kurt foobar
2013-01-03 2013-01-03 NaN 5 Carl foobar
2013-01-04 2013-01-04 NaN 2 Mary foobar
2013-01-05 2013-01-05 NaN 8 Alfred foobar
2013-01-06 2013-01-06 NaN 1 Tom foobar
Editing answer
df = pd.DataFrame(data)
df.set_index("Date_Range", inplace=True)
print(df)
print(df['Series'])
Series has Index, u need to define Dataframe First and then set Index.
Output:
Date_Range Series Int_Array Name Constant
2013-01-01 start 3 John foobar
2013-01-02 2 7 Kurt foobar
2013-01-03 5.6 5 Carl foobar
2013-01-04 2020-01-31 00:00:00 2 Mary foobar
2013-01-05 True 8 Alfred foobar
2013-01-06 last 1 Tom foobar
Output 2:
Date_Range
2013-01-01 start
2013-01-02 2
2013-01-03 5.6
2013-01-04 2020-01-31 00:00:00
2013-01-05 True
2013-01-06 last
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.