@@ -1771,7 +1771,7 @@ def to_numpy(
1771
1771
1772
1772
return result
1773
1773
1774
- def to_dict (self , orient : str = "dict" , into = dict ):
1774
+ def to_dict (self , orient : str = "dict" , into = dict , index = True ):
1775
1775
"""
1776
1776
Convert the DataFrame to a dictionary.
1777
1777
@@ -1807,6 +1807,11 @@ def to_dict(self, orient: str = "dict", into=dict):
1807
1807
instance of the mapping type you want. If you want a
1808
1808
collections.defaultdict, you must pass it initialized.
1809
1809
1810
+ index : bool default True
1811
+ When set to False, method returns a dict without an index key
1812
+ (and index_names if using orient='tight'). Can only be False
1813
+ when orient='split' or 'tight'.
1814
+
1810
1815
Returns
1811
1816
-------
1812
1817
dict, list or collections.abc.Mapping
@@ -1909,43 +1914,77 @@ def to_dict(self, orient: str = "dict", into=dict):
1909
1914
elif orient .startswith ("i" ):
1910
1915
orient = "index"
1911
1916
1917
+ if not index and orient not in ['split' , 'tight' ]:
1918
+ raise ValueError (
1919
+ "'index=False' is only valid when 'orient' is 'split' or 'tight"
1920
+ )
1921
+
1912
1922
if orient == "dict" :
1913
1923
return into_c ((k , v .to_dict (into )) for k , v in self .items ())
1914
1924
1915
1925
elif orient == "list" :
1916
1926
return into_c ((k , v .tolist ()) for k , v in self .items ())
1917
1927
1918
1928
elif orient == "split" :
1919
- return into_c (
1920
- (
1921
- ("index" , self .index .tolist ()),
1922
- ("columns" , self .columns .tolist ()),
1929
+ if not index :
1930
+ return into_c (
1923
1931
(
1924
- "data" ,
1925
- [
1926
- list (map (maybe_box_native , t ))
1927
- for t in self .itertuples (index = False , name = None )
1928
- ],
1929
- ),
1932
+ ("columns" , self .columns .tolist ()),
1933
+ (
1934
+ "data" ,
1935
+ [
1936
+ list (map (maybe_box_native , t ))
1937
+ for t in self .itertuples (index = False , name = None )
1938
+ ],
1939
+ ),
1940
+ )
1941
+ )
1942
+ else :
1943
+ return into_c (
1944
+ (
1945
+ ("index" , self .index .tolist ()),
1946
+ ("columns" , self .columns .tolist ()),
1947
+ (
1948
+ "data" ,
1949
+ [
1950
+ list (map (maybe_box_native , t ))
1951
+ for t in self .itertuples (index = False , name = None )
1952
+ ],
1953
+ ),
1954
+ )
1930
1955
)
1931
- )
1932
1956
1933
1957
elif orient == "tight" :
1934
- return into_c (
1935
- (
1936
- ("index" , self .index .tolist ()),
1937
- ("columns" , self .columns .tolist ()),
1958
+ if not index :
1959
+ return into_c (
1960
+ (
1961
+ ("columns" , self .columns .tolist ()),
1962
+ (
1963
+ "data" ,
1964
+ [
1965
+ list (map (maybe_box_native , t ))
1966
+ for t in self .itertuples (index = False , name = None )
1967
+ ],
1968
+ ),
1969
+ ("column_names" , list (self .columns .names )),
1970
+ )
1971
+ )
1972
+ else :
1973
+ return into_c (
1938
1974
(
1939
- "data" ,
1940
- [
1941
- list (map (maybe_box_native , t ))
1942
- for t in self .itertuples (index = False , name = None )
1943
- ],
1944
- ),
1945
- ("index_names" , list (self .index .names )),
1946
- ("column_names" , list (self .columns .names )),
1975
+ ("index" , self .index .tolist ()),
1976
+ ("columns" , self .columns .tolist ()),
1977
+ (
1978
+ "data" ,
1979
+ [
1980
+ list (map (maybe_box_native , t ))
1981
+ for t in self .itertuples (index = False , name = None )
1982
+ ],
1983
+ ),
1984
+ ("index_names" , list (self .index .names )),
1985
+ ("column_names" , list (self .columns .names )),
1986
+ )
1947
1987
)
1948
- )
1949
1988
1950
1989
elif orient == "series" :
1951
1990
return into_c ((k , v ) for k , v in self .items ())
0 commit comments