Skip to content


Creating pandas dataframes

python, pandas1 min read

Pandas dataframe is a 2D data structure which looks like a table. There are different ways to create a dataframe, mostly everybody sticks to few formats they like and stick to it, thats good. But, sometimes when you have a problem and you want somebody's opinion or get a suggestion or solution, you cannot give all your code and ask them to execute and start explaining the problem. Its a lot easier if you could generate a small sample data and explain it and from suggestion/solution you could copy the concept to your main program.

Whenever you ask a question in StackOverflow or any forum and when preparing to ask the question itself, you would know that it would be a lot easier if you provided a sample data and say this the problem you are facing and this what i have tried, so far ? In python pandas world, you need to build small dataframes to build those examples.

Here are few ways of creating new dataframes and generating data.

  1. Empty dataframe
  2. Tuple technique
  3. Dictionary with value lists
  4. Array of Arrays / Lists of List
  5. Array of dictionary
  6. pd.Series to dataframe
  7. 8x4 dataframe with some random data
  8. Single column list of alphabets
  9. dictionary to Dataframe

Empty dataframe

Empty Dataframe with columns

Tuple technique

Here index is the first tuple variable

Dictionary with value lists

Array of Arrays / Lists of List

Array of dictionary

pd.Series to dataframe

8x4 dataframe with some random data

Single column list of alphabets

dictionary to Dataframe

Here we create dataframe from a complex dictionary