Convert Dataframe To Tuple And Then Back Into Dataframe In Python Pandas
I am looking to get a dataframe back from a list of tuples using the following: tuples = tuple(df.itertuples(index=False)) #do some code, passing out df ls = [(tup._fields, pd.Seri
Solution 1:
Why are you extracting the tup._fields line? You can convert back with a list of NamedTuple.
Consider this example:
df = pd.DataFrame({'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])})
tuples = tuple(df.itertuples(index=False))
df2 = pd.DataFrame(list(tuples))
df2.head()
#> one two#> 0 1.0 1.0#> 1 2.0 2.0#> 2 3.0 3.0#> 3 NaN 4.0
Post a Comment for "Convert Dataframe To Tuple And Then Back Into Dataframe In Python Pandas"